Scope via global

Started by Lutz, June 16, 2008, 08:11:54 AM

Previous topic - Next topic

Lutz

this is a response to DrDave to a post in the news thread:



You can overwrite global symbols inside a namespace just as you can with built-in primitives, which are also globals. To overwrite a global symbol you would prefix it with the context name, when used first. newLISP will assume the global version only if a local one does not exist.


(set (global 'x) 123)

(context 'CTX)
(println x)

(set 'CTX:x 456)
(println x)

(println MAIN:x)

(context 'MAIN)
(exit)


if you run this script:


~> newlisp overwrite
123
456
123
~>


Note that all symbols are created while loading/translating. There is no dynamic jumping between both versions during runtime. The context statement only influences code translation.



Generally overwriting global symbols is not recommended, as it can create confusing code. But there are instances, where it is necessary.