[REBOL] Re: Binding
From: brett:codeconscious at: 20-Nov-2002 3:18
Hi Robert,
> Hi, ok I try again with the goal to finally understand this. Can't
> believe it but even after years of Reboling it sometimes surprises me...
> And I just read some of your words, etc. stuff from your Rebol page. But
> it's really hard stuff...
I leave it to other to explain that code in more detail. I wanted to provide
some different examples of words and their bindings just in case they help
in understanding. I recommend pasting the script below into the console.
Regards,
Brett.
; The following four lines when executead establish the
; word "greeting" into four different contexts.
greeting: {Hi!}
australia: context [ greeting: {G'day!} greet: does [greeting] ]
france: context [ greeting: {Salut!} greet: does [greeting] ]
sweden: context [ greeting: {Hey!} greet: does [greeting] ]
; instances-of-greeting will contain four values
; of type word! all of which have the same name {greeting}
; but which differ in that each is bound to a different context.
instances-of-greeting: reduce [
'greeting
in australia 'greeting
in france 'greeting
in sweden 'greeting
]
; Visually we might think it is the same word, but internally
; REBOL knows to keep them seperate.
?? instances-of-greeting
reduce instances-of-greeting
; We can create an expression that will evaluate to a single value.
expression: compose/deep [ rejoin [(instances-of-greeting)] ]
; Which can be evaluated like
do expression
; But just because it looks like code doesn't mean we can't treat it
; as data.
get expression/rejoin/1
get expression/rejoin/2
; We can make a function from the expression by using the expression as
; the function body.
f: does expression
f
; Or demonstrate how the words in a function body are bound to the
; function arguments.
g: func [ greeting ] expression
g "Yo"
; Which is like using BIND on the expression itself - lets bind it to
; the swedish context.
new-expression: bind/copy expression in sweden 'self
do new-expression
; BIND changes the context of words in a block, so does FUNC to its
; body block. CONTEXT (make object!) also changes the context of
; words in a block it is given.
; In this next example, treat the script block as data like we did
; before.
currency-name: "Dollar"
script: [
euro-zone: context [
currency-name: "Euro"
print-currency: does [print currency-name]
]
]
get script/context/does/2
; Now see the change after the script block is evaluated.
do script
get script/context/does/2
; End of examples.