Code: Select all
(context 'foo)
(set 'a 10)
(context MAIN)
In Javascript, you can do some_function.apply(foo, args), which is the same as:
Code: Select all
foo.fn = some_function;
foo.fn(args);
delete foo.fn;
Code: Select all
(context 'foo)
(set 'a 10)
(context MAIN)
Code: Select all
foo.fn = some_function;
foo.fn(args);
delete foo.fn;
Code: Select all
(context 'foo)
(set 'a 10)
(context 'bar)
(set 'a 20)
(context MAIN)
(define (p c s)
(println (eval (sym s c))))
(p 'foo 'a)
; 10
(p 'bar 'a)
; 20
Code: Select all
(set 'foo:a 10)
(set 'a 20)
(define (f) (println a))
(def-new 'f 'foo:f)
(foo:f)
(delete 'foo:f)
no, all symbols occuring in your function are bound during code translation, not application.I'm talking about dynamically binding a function to a context for the period of execution
Code: Select all
(set 'x "hello")
"hello"
> (set 'x "main hello")
"main hello"
> (set 'ctx:x "ctx hello")
"ctx hello"
> (define (foo) (println x))
(lambda () (println x))
> (set 'ctx:foo foo)
(lambda () (println x))
> (ctx:foo)
main hello
"main hello"
> (foo)
main hello
"main hello"
>
yes, they create new symbols in the target contextDoes def-new and new bind new symbols in the target context, then?
yes, you can build lambda expressions dynamically, just like you can create lists dynamically, but using existing symbols.What about a dynamically built lambda or a lambda templated from a macro?
yes, for example in this case:Are their symbols late-bound in the context in which they were created?
Code: Select all
> (define (foo ctx value) (set 'ctx:data value))
(lambda (ctx value) (set 'ctx:data value))
>
> (context 'myctx) (context MAIN)
myctx
MAIN
> (foo myctx 123)
123
> myctx:data
123
>
Event-based programming is very much related to OO programming and message passing. Originally OO programming was developed for implementening event based programs (see the Simula programming language). In the new newLISP OOP model you could see objects as implementing different scopes receiving a message:The ability to dynamically bind a function to the scope desired makes event-based programming efficient and elegant.
Code: Select all
(:message object)
Code: Select all
(define (report-message)
(print x))
(define (object-a f (x "hello"))
(f x))
(define (object-b f (x "world"))
(f x))
(object-a report-message) ; outputs "hello"
(object-b report-message) ; outputs "world"
Code: Select all
(context 'foo)
(set 'x 5)
(define (ctx-eval-string)
(apply eval-string (args)))
(context 'bar)
(set 'x 10)
(define (ctx-eval-string)
(apply eval-string (args)))
(context MAIN)
(define (f) x)
(define (g a) (* a x))
(define (fn->ctx-fn _ctx _f)
(apply (sym 'ctx-eval-string _ctx) (list (string _f))))
Code: Select all
> f
(lambda () x)
> g
(lambda (a) (* a x))
> (fn->ctx-fn 'foo f)
(lambda () foo:x)
> (fn->ctx-fn 'bar f)
(lambda () bar:x)
> (fn->ctx-fn 'foo g)
(lambda (foo:a) (* foo:a foo:x))
> (fn->ctx-fn 'bar g)
(lambda (bar:a) (* bar:a bar:x))
>
> ((fn->ctx-fn 'foo f))
5
> ((fn->ctx-fn 'foo g) 10)
50
> ((fn->ctx-fn 'bar f))
10
> ((fn->ctx-fn 'bar g) 10)
100
>
> (set 'myg (fn->ctx-fn 'foo g))
(lambda (foo:a) (* foo:a foo:x))
> (myg 10)
50