There's a workaround to avoid context machinery inside the funcion body when defining functions, the idea is to use the dictionary facility of contexts.
If we accept to use variables in the context as dictionary values we can easy use them without worrying about context issue, the prize to pay is to treat variables as strings in the context dictionary.
To do this we redefine our mk-ctx function this way:
The idea is to use (MY "var") everywhere we refer to a context variable var (you can use MY or whatever word you prefer providing it is capitalized) and to expand that expression inside function body to get the value stored in the context dictionary.
The use is so simple:
If we accept to use variables in the context as dictionary values we can easy use them without worrying about context issue, the prize to pay is to treat variables as strings in the context dictionary.
To do this we redefine our mk-ctx function this way:
Code Select
(define (mk-ctx c f b) (context c f (expand b '((MY (context)))) ) (context c))
The idea is to use (MY "var") everywhere we refer to a context variable var (you can use MY or whatever word you prefer providing it is capitalized) and to expand that expression inside function body to get the value stored in the context dictionary.
The use is so simple:
Code Select
(setq the-ctx (mk-ctx OO 'x2 (lambda () (* 2 (MY "x")))
(the-ctx "x" 33)
(the-ctx:x2) ---> 66
(setq the-ctx (mk-ctx OO 'f (lambda () 1) ))
(the-ctx:f) --> 1