About contexts and context evaluation

Pondering the philosophy behind the language
Locked
Maurizio
Posts: 52
Joined: Mon Jul 28, 2003 3:06 pm
Location: Italy

About contexts and context evaluation

Post by 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
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

In the example, the Account:new procedure

Code: Select all

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

Code: Select all

(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

Code: Select all

(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

Code: Select all

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

Code: Select all

(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
Posts: 52
Joined: Mon Jul 28, 2003 3:06 pm
Location: Italy

Post by Maurizio »

in this case, wouldn't

(set 'ctx ctx)

do the same thing as

(set 'ctx (eval ctx))

?
Regards
Maurizio

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

Post by Lutz »

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:

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
See the method as explained at the beginning of the chapter "Programming with context objects", which is streight forward.

Lutz

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

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

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

Post by Lutz »

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
Posts: 52
Joined: Mon Jul 28, 2003 3:06 pm
Location: Italy

Post by Maurizio »

Thank you very much
now it's a little clearer...
Regards
Maurizio.

Locked