delete symbol from context?

For the Compleat Fan
Locked
nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

delete symbol from context?

Post by nigelbrown »

Can a symbol be deleted from a context some way?
The reason for doing so may be that if a symbol is
created in a context before the same symbol is made global in main then the globalized symbol is never global to that context (can be accessed by MAIN:symbol of course). If that symbol could be deleted from the context symbol table then, presumably, the global symbol could be seen. This would allow cleaning up and in extreme cases perhaps save some memory.
Could a whole context be deleted (currently they are protected in main) once no longer needed eg a context that does only initialization stuff?
This reminds me of the FORTH forget of some of its dictionary.

Nigel

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

yes, you can:

(set 'CTX:var 123) ;; creates context CTX and a symbol 'var

(symbols CTX) => (CTX:var) ;; one symbol in context CTX

(delete 'CTX) => nil ;; cannot delete context because it has a symbol

(delete 'CTX:var) => true ;; you can delete the variable

(delete 'CTX) => true ;; now you can delete the context, because its empty


Variables referenced in other expressions will be replaced with 'nil' when deleted.

Imagine several variables:

(set 'CTX:var1 123)
(set 'CTX:var2 345)

(symbols CTX) => (CTX:var1 CTX:var2)

(map delete (symbols CTX)) => (true true) ;; delete all vars in a context

(delete 'CTX) => true ;; can delete the empty context

Lutz

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Thanks Lutz

Locked