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

[REBOL] Re: Binding a changed function within a context

From: brett:codeconscious at: 6-Oct-2002 15:24

For amusement, below are some examples of words bound to different contexts. Just move throught the code by copying from the email and pasting bits of it at a time into the console. Regards, Brett. ; ; Amusement 1 ; ; BIND matches up words it can find in the target context. ctx-1: context [country: "Australia"] ctx-2: context [city: "Sydney"] ctx-3: context [name: "Brett"] expression: [join name [{ lives in } city {, } country]] repeat ctx reduce [ctx-1 ctx-2 ctx-3] [bind expression in ctx 'self] print expression ; ; Functions create a new context binding the words of their body block ; to their arguments: my-func: func [name] expression my-func "My wife" ; ; Amusement 2 ; ; This first bit is straight forward - what appears to be ; nesting of contexts. As others have pointed out (probably ; more clearly than me) this is an effective or apparent ; nesting. ctx-1: context [ word-a: #1a word-b: #1b word-c: #1c ctx-2: context [ word-a: #2a word-b: #2b ctx-3: context [ word-a: #3a set 'print-it does [ print ["First function" word-a word-b word-c] ] ] ] ] print-it ; ; We can simulate the apparent scoping of CONTEXT with ; script that is obviously not nested... ; ctx-1: context [word-a: #1a word-b: #1b word-c: #1c] ctx-2: context [word-a: #2a word-b: #2b] ctx-3: context [word-a: #3a] contexts: reduce [ctx-1 ctx-2 ctx-3] expression: [print ["Expression" word-a word-b word-c]] repeat ctx contexts [bind expression in ctx 'self] do expression ; ; Or indeed reverse it, or perhaps do other odd ; bindings. ; reverse contexts repeat ctx contexts [bind expression in ctx 'self] do expression ; ; Amusement 3 ; ; REBOL represents words to you using ; their names but does not have a visual representation for ; the context of those words. ; When you print the mold of the expression of this example ; you cannot see the difference between the two words that ; have the same name 'sample-word. ; Philosophical point: ; It is easy to fall into the trap of mistaking the name ; of a thing for the thing itself. As always with REBOL, ; the items you see in the console or in a file are not ; the things themselves but representations of the things. ctx-1: context [sample-word: "Context1"] ctx-2: context [sample-word: "Context2"] expression: [{A word: }] append expression in ctx-1 'sample-word append expression [{^/A different word with the same name: }] append expression in ctx-2 'sample-word print expression ; This last example was admittedly contrived, and you ; wouldn't expect to see it used much. But I think ; it has value in showing a different way to look at ; REBOL words.