Question about the "def-new" function.

Q&A's, tips, howto's
Locked
PaipoJim
Posts: 20
Joined: Fri Jun 03, 2005 3:21 pm
Location: Oregon

Question about the "def-new" function.

Post by PaipoJim »

I am trying to write a function that creates a number of contexts of the same type all with the same default function, a sort of class constructor method, if you will. It seems possible but I can't get it to work (at least without a macro).

Code: Select all

> (define (myProc) (println "hi"))
(lambda () (println "hi")) 

> (def-new 'myProc 'myCtx:myCtx)
myCtx:myCtx

> (myCtx:myCtx)
hi
"hi"

> (context? myCtx)
true

> myCtx:myCtx
(lambda () (println "hi"))
Great! So let's do it from a variable:

Code: Select all

> (set 'myVar 'myCtx2:myCtx2)
myCtx2:myCtx2
 
> (def-new 'myProc myVar)
myCtx2:myCtx2
 
> (context myCtx2)
myCtx2 

myCtx2> (myCtx2)
hi
"hi"
So far so good, let's set the variable programatically:

Code: Select all

> (set 'myVar (sym (append "myCtx" (string 3) ":myCtx" (string 3))))
myCtx3:myCtx3
 
> (def-new 'myProc myVar)
myCtx3:myCtx3
 
> (context? myCtx3)
nil 

> myCtx3:myCtx3

context expected : myCtx3
How do I set myVar so that "def-new" evaluates it in the same way as when I set it to 'myCtx2:myCtx2 explicitly?

Do I indeed need a macro for this or is it something I'm just not getting about symbols and newLISP's evaluation of variables?
-

Locked