How to overload and save your old function
Posted: Thu Aug 25, 2005 6:27 am
Hello!
I have been thinking about it for some time and there seems to be the way how to overload and also save your old function when the new newLISP comes in.
Now you can try:
> (add 5 2)
107
> (old-add 5 2)
7
(if you run the code above twice and try (add 5 2), you get the call stack overflow - functions run in the endless recursion)
(if you run the code above twice, you get collision between (constant 'shuffle) and (define (shuffle)) - nothing special ;-)
Hope you like it, Fanda
ps: As Lutz pointed out, you can also use (constant (global '+) add) to make it global ;-)
I have been thinking about it for some time and there seems to be the way how to overload and also save your old function when the new newLISP comes in.
Code: Select all
;
; Overload the old function
;
(constant 'old-add add)
(define (new-add x y)
(old-add x y 100))
(constant 'add new-add)
> (add 5 2)
107
> (old-add 5 2)
7
(if you run the code above twice and try (add 5 2), you get the call stack overflow - functions run in the endless recursion)
Code: Select all
;
; Save my old function when the new version of newLISP comes in
;
; my old function
(define (shuffle x)
(- x))
; when the new version comes in, rename it to my-shuffle
(define (my-shuffle x)
(- x))
; save the new newLISP function and reassign shuffle to my-shuffle
(constant 'new-shuffle shuffle)
(constant 'shuffle my-shuffle)
; my old code should still work and the new shuffle is as 'new-shuffle'
(println (shuffle 1))
(println (shuffle -5))
Hope you like it, Fanda
ps: As Lutz pointed out, you can also use (constant (global '+) add) to make it global ;-)