Contexts and Symbols

Started by statik, February 11, 2006, 04:48:39 PM

Previous topic - Next topic

statik

What would you suggest I mold into a habit?



Using a context prefix:

(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:



(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

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



(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.:



(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

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