Contexts and Symbols

Q&A's, tips, howto's
Locked
statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Contexts and Symbols

Post by statik »

What would you suggest I mold into a habit?

Using a context prefix:

Code: Select all

(context 'MYCONTEXT)
(define (myfunc , )
  (println MAIN:a)
)

(context 'MAIN)
(setq a 10)
(MYCONTEXT:myfunc)
or passing the value of a symbol when calling a function from another context:

Code: Select all

(context 'MYCONTEXT)
(define (myfunc value , )
  (println myfunc)
)

(context 'MAIN)
(setq a 10)
(MYCONTEXT:myfunc a)
Is there anything I should keep in mind when using them?
-statik

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

Post by Lutz »

Your second method is definitely the way to do it, and I think you had a little typo: 'myfunc' instead of 'value':

Code: Select all

(context 'MYCONTEXT)
(define (myfunc value)
  (println value)
)

(context 'MAIN)
(setq a 10)
(MYCONTEXT:myfunc a)
You tell MYCONTEXT:myfunc explicitly what to print, in your first version this is hidden inside MYCONTEXT and MYCONTEXT:myfunc is accessing a variable in MAIN behind the curtain.

In your second version you deliver the argument to print as a parameter of MYCONTEXT:myfunc whih is the correct way to do it.

Your first way would be correct if MAIN:a is some kind of global setting, which does not change frequently, i.e.:

Code: Select all

(context 'MYCONTEXT)

(define (report value)
    (print-using MAIN:printer value))

(context MAIN)

(setq printer "HP_DESKJET")

(MYCONTEXT:report value) 
In this last example it makes sense for MYCONTEXT:report to access MAIN:printer as a global configuration variable, and not pass it as a parameter.

Lutz

statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post by statik »

Yes, you were correct in assuming a typo. Thanks for the help, that clarifies a lot for me.
-statik

Locked