setguard
setguard(s_symbolg_guard) =>u_guard
Description
Mainly enforces disciplined use of a symbol as a global variable by associating it with a guarding function that is either a symbol that identifies the name of the guarding function or a lambda form (just like the first argument to the apply function). If the guarding function is nil, the symbol is unguarded. The guarding function is called with two arguments whenever a new value is assigned to the symbol: the symbol and the value to be assigned to it. The result of the guarding function determines the setguard return value that gets assigned to the symbol.
The guarding function associated with a guarded symbol is triggered whenever a new value is assigned to that symbol by way of the setq (or set) function. Neither a lambda binding nor a let binding will cause the guarding function to be called (see examples below).
Arguments
Value Returned
|
Either a symbol that identifies the name of the guarding function or a function object. |
Examples
> procedure( myPortGuard(varName newValue)
if( portp(newValue)
then
newValue
else
printf("Only port values can be assigned to `%s'\n" varName)
symeval(varName)
)
)
myPortGuard
> setguard('poport 'myPortGuard)
myPortGuard
> poport = nil
Only port values can be assigned to `poport'
port:"*stdout*"
> poport = 123
Only port values can be assigned to `poport'
port:"*stdout*"
> setguard( 'myStringVar
lambda((varName newValue)
if(stringp(newValue)
then
newValue
else
printf("Only strings can be assigned to `%s'\n" varName)
symeval(varName)
)
) ; lambda
) ; setguard
> myStringVar = "default"
"default"
> myStringVar = 123
Only strings can be assigned to `myStringVar'
"default"
> myStringVar = nil
Only strings can be assigned to `myStringVar'
"default"
;; A lambda binding will not trigger the guard
> ((lambda (myStringVar) (println 'hello)) nil)
hello
nil
;; A let binding will also not trigger the guard
> let( ((myStringVar 123))
println(myStringVar)
)
123
nil
;; This s the symbol `myStringVar' unguarded
> setguard('myStringVar nil)
nil
> myStringVar = 123
123
Related Topics
Return to top