Scope via global

For the Compleat Fan
Locked
Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Scope via global

Post by 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.

Code: Select all

(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:

Code: Select all

~> 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.

Locked