About contexts and context evaluation

Started by Maurizio, June 03, 2004, 02:27:32 AM

Previous topic - Next topic

Maurizio

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

Sammo

#1
In the example, the Account:new procedure


define (Account:new ctx nme bal ph) ...)
expects in variable ctx the name of a context to be created. The expression


(MAIN:new Account ctx)
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


(set 'ctx (eval ctx))
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


(set 'TheContextItself (eval ctx))
in which case the succeeding lines would have been written:


(set 'TheContextItself:full-name nme)
(set 'TheContextItself:balance bal)
(set 'TheContextItself:phone ph))
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.

Maurizio

#2
in this case, wouldn't



  (set 'ctx ctx)



do the same thing as



  (set 'ctx (eval ctx))



?

Regards

Maurizio

Lutz

#3
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:

(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


See the method as explained at the beginning of the chapter "Programming with context objects", which is streight forward.



Lutz

Lutz

#4
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

Lutz

#5
I just realize there are 3 other folks on the board! But I gotta go now and will not be online until tonight.



Lutz

Maurizio

#6
Thank you very much

now it's a little clearer...

Regards

Maurizio.