O no! Those contexts again!

Started by pjot, May 10, 2005, 02:41:41 PM

Previous topic - Next topic

pjot

Hi,



Again I am struggling with contexts. It seems not to be possible to change variables in other contexts while being in a newly, just created context...?



Sample program:

(context 'START)

# Create context "HELLO" and initialize list "mylist"
(define (init)
(new MAIN:START (sym "HELLO") true)
(push "HELLO" mylist))

# Add a value to the list in context "START"
(define (test)
(push "yes" START:mylist)
(println "2: " START:mylist))

#-----------------------------

(context 'MAIN)

# Setup problem
(START:init)
(println "1: " START:mylist)

# Add value to "mylist" from created context
(HELLO:test)  # <----- (START:test) works

# Nothing happened?
(println "3: " START:mylist)

(exit)


So, first I create a new context, and from that newly created context 'HELLO' I try to reach the list "mylist" in context 'START'. But this seems to be impossible.



What am I doing wrong here?



If I replace the line (HELLO:test) with (START:test) I receive the correct result.

Lutz

#1
When you copy the context 'START to 'HELLO it will not respect the START:mylist but create a HELLO:mylist, because mylist is part of START and and you told it to make a copy of START, so it will replicate all symbols from START into HELLO. THE "yes" will end up in HELLO:mylist.



So after running your program you get (commented out the exit):



> START:mylist

("HELLO")

> HELLO:mylist

("yes")

>



START works like a template for an identically created HELLO context. I think you are trying to imitate the 'Class' funcionality of C++, Java or Smalltalk, where functions are owned by the class and class-variables are possible. There is no such thing in template based OO. The new objects are just copies where everyting: functions and data structured are instantiated in the new object/context.



Lutz

pjot

#2
Thanks. I followed the newLisp documentation, were it is stated that:


Quote
To refer to a symbol outside its context, it has to be preceded by the context name and a colon, e.g.:



> FOO:x


Since I am able to reach the symbol "mylist" from the MAIN context using "START:mylist", it seemed logic to me that a similar thing is possible from any other context as well. So querying "START:mylist" from the 'HELLO' context is expected to deliver the same result...



Obviously, my assumption was wrong... :-(... Why is it that the 'mylist' is duplicated in a way, that even a context-reference to it is blocked? Since indeed, it is not really an external object anymore, it seems.



 Chapter 16 of the newLisp manual "Programming with context objects" at least suggests that contexts are objects. You mention it is "prototype based", but also:


Quote
Note that OO programming purists would use getter and setter functions to access object variables. This is not necessary in newLISP as context variables are still accessible prefixing the variables with the context/object name and user-defined functions in a context would occupy space in each instance of a context object. Using new mixins of different contexts into one are possible.


This line is only true for the contexts which are statically already in the sourceprogram then?



Peter

Lutz

#3
Its just that the 'new' funtion while copying translates all references to the old context into references in the new context.



You can reference data or functions in other contexts:



(context 'ACTX)

(set 'mylist '())

(context 'MAIN)

(context 'BCTX)

  (define (foo x)
    (push x ACTX:mylist))  

  (define (clone-me s)
        (new BCTX s))    

(context 'MAIN)


After loading this program you could do:



newLISP v.8.5.7 on OSX, execute 'newlisp -h' for more info.

> (BCTX:foo 123)
123
> ACTX:mylist
(123)
> (BCTX:clone-me 'CCTX)
CCTX
> (CCTX:foo 456)
456
> ACTX:mylist
(456 123)
>


This time BCTX:foo was copied to CCTX:foo keeping the reference to ACTX unchanged because ACTX was a foreign-context reference.



So everything works as advertised ;)



Lutz

pjot

#4
OK thanks.



So if I understand it all well: I need to put the list in an external context 'EXTERNAL, then create a new context in which the reference to this external context 'EXTERNAL also is copied.



I'll do that!



Thanks again,

Peter