Looking in the manual, in the parahraph Context as classes,
I see the following statement :
(set 'ctx (eval ctx)) ;; get context out of symbol
I really don't understand it.
Any explanation ?
Regards
Maurizio
About contexts and context evaluation
In the example, the Account:new procedure
expects in variable ctx the name of a context to be created. The expression
creates the new context by copying the existing Account context. The parameter ctx is still the name of the context, not the context itself. The expression
fetches the context itself by evaluating the name and assigns the context to the same variable. Instead of reusing the same variable to hold now the context, Lutz could have written
in which case the succeeding lines would have been written:
Bottom line: When referencing context-based object variables, it is necessary to refer to the object (i.e., context) itself and not just the object's (context's) name.
Code: Select all
define (Account:new ctx nme bal ph) ...)
Code: Select all
(MAIN:new Account ctx)
Code: Select all
(set 'ctx (eval ctx))
Code: Select all
(set 'TheContextItself (eval ctx))
Code: Select all
(set 'TheContextItself:full-name nme)
(set 'TheContextItself:balance bal)
(set 'TheContextItself:phone ph))
What Sam explained is correct. What the Account:new function receives in its variable ctx is a symbol of a context, which then gets created with MAIN:new. So ctx contains a symbol. Doing (set 'ctx (eval ctx)) gets to the context inside the symbol. Its like saying:
(set 'x 123) => 123
(set 'y 'x) => x
(set 'y (eval y)) => 123
I probably made this example too complicated. I was trying to mimic a traditional Class with its own 'new' method which can take initializers. Instead you could define an initializer function to set name, balance, telephone to certain values:
See the method as explained at the beginning of the chapter "Programming with context objects", which is streight forward.
Lutz
(set 'x 123) => 123
(set 'y 'x) => x
(set 'y (eval y)) => 123
I probably made this example too complicated. I was trying to mimic a traditional Class with its own 'new' method which can take initializers. Instead you could define an initializer function to set name, balance, telephone to certain values:
Code: Select all
(context 'Account)
(define (init nme bal tel)
(set 'name nme)
(set 'balance bal)
(set 'telephone tel))
(define (deposit amount)
(inc 'balance amount))
(context 'Main)
(new Account 'JD-001) ;; create account
(JD001:init "John Doe" 123.45 "555-55-1212") ;; initialize it
(JD001:deposit 100) ;; make deposit
Lutz
saying:
(set 'ctx ctx)
is not the same thing because ctx would again only contain the symbol ctx not the context inside the variable symbol:
(set 'ctx 'MAIN) ;; store a context symbol in ctx
(context? ctx) => nil ;; ctx does not contain a context
(symbol? ctx) => true ;; ctx contains a symbol
(set 'ctx (eval ctx)) ;; unpeel the context
(context? ctx) => true
It's just the difference of storing a variable symbol or the value the variable symbol contains. The value in this case is a context (not a number)
Lutz
(set 'ctx ctx)
is not the same thing because ctx would again only contain the symbol ctx not the context inside the variable symbol:
(set 'ctx 'MAIN) ;; store a context symbol in ctx
(context? ctx) => nil ;; ctx does not contain a context
(symbol? ctx) => true ;; ctx contains a symbol
(set 'ctx (eval ctx)) ;; unpeel the context
(context? ctx) => true
It's just the difference of storing a variable symbol or the value the variable symbol contains. The value in this case is a context (not a number)
Lutz