Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] objects without overhead Re:(3)

From: joel:neely:fedex at: 18-Oct-2000 7:22

Hi, Rishi, [rishi--picostar--com] wrote:
> state: make object! [ > new: func [ > puzzle [block!] > ][ > > return make object! [ > puz: copy reduce puzzle > get-puzzle: :get_puzz > ] > ] > get_puzz: does [return copy puz] > test-obj: new [1 2 3 4 0 5 6 7 8] > print test-obj/get-puzzle > ] >
> I don't understand why get-puzzle cannot access word 'puz. >
Look at the following (awful ASCII art) diagram, which shows the contexts and references of the various names involved: (global) | +--------------...--+ | | state==>(object!) (etc) | +--------+-----------+ | | | new | test-obj==>(object!) | | | puzzle | +----------+ | | | get_puzz<==get-puzzle puz As the above code is being evaluated, look at the contexts that are established, and the words they contain. Context for... Contains words.... ---------------- --------------------------------------------- (REBOL global) 'state (along with whatever else was there) STATE 'new 'get_puzz 'test-obj (the object's "members") NEW 'puzzle (the function's argument) Now, the next-to-last line inside STATE makes 'test-obj (in the context of STATE) refer to a newly-created object which has the following context: TEST-OBJ 'get-puzzle 'puz Here's the critical issue: TEST-OBJ refers to an object whose component GET-PUZZLE refers to the *value* of GET_PUZZ, which contains words defined *in the context of its own definition* and in that context, *there is no 'puz*. Remember that 'puz is defined in the context of the object to which TEST-OBJ now refers, but *not* in the context of STATE. If you'll pardon the anthropomorphism, TEST-OBJ knows both GET-PUZZLE and PUZ, and introduced GET-PUZZLE to GET_PUZZ, but nobody ever introduced GET_PUZZ to PUZ. What you were trying to do would only have succeeded if REBOL used "dynamic scoping" such as Lisp or xBase use, in which new variables are added to the global "environment" and are visible to everyone after (chronologically!!!) the point of definition. REBOL contexts don't behave that way. Hope this helps! -jn-