[REBOL] Re: Binding
From: robert:muench:robertmuench at: 20-Nov-2002 9:36
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
> On Behalf Of Brett Handley
> Sent: Tuesday, November 19, 2002 5:18 PM
> To: [rebol-list--rebol--com]
> Subject: [REBOL] Re: Binding
> I leave it to other to explain that code in more detail.
Hi, I do it for training :-))
> 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.
Thanks a lot. Very good stuff!
> ; The following four lines when executead establish the
> ; word "greeting" into four different contexts.
Ok, so far I got it ;-)) BTW: As I'm still doing C++ programming and
switching brain between these two isn't smooth all the time I think of
CONTEXT of something like a namespace. The only hard part is that you
don't have to use fully qualified names in Rebol. So you don't see it.
> ; 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.
Ok, that's clear too.
> ; Visually we might think it is the same word, but internally
> ; REBOL knows to keep them seperate.
Yep.
> expression: compose/deep [ rejoin [(instances-of-greeting)] ]
I have question here: How does the evaluation sequence look like? Will
compose/deep (a) evaluate instance-of-greeting and replace the parens
with the result or (b) keep a live reference to the different greeting
so that I even could change the value?
> ; But just because it looks like code doesn't mean we can't
> ; treat it as data.
;-) That's cool and I'm not used to it...
> ; We can make a function from the expression by using the
> ; expression as the function body.
Nice too.
> ; Or demonstrate how the words in a function body are bound
> ; to the function arguments.
This sounds logical but it's really tricky I think. This means that
EXPRESSION is switching clothes. I mean, if you think of the words as
pointers to some value, in C the pointers would stay the same. No matter
where you look at the pointes. In Rebol the pointers get new values
depending of the context ;-) you read them out. Pretty nice, but very
complex to understand and even more complex to keep track of.
> 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
For GREETING it's now clear what happens. But what happens for REJOIN? I
mean this word isn't in the swedish context. So Rebol key-words must
somehow be marked so that BIND knowns to skip these. Is it this way?
> ; BIND changes the context of words in a block
Ok, to the context you specify through the KNOWN-WORD argument.
> so does FUNC to its body block.
And here, to what context are those words bound?
> 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
== "Dollar" because executed in the "global" context.
> ; Now see the change after the script block is evaluated.
>
> do script
> get script/context/does/2
Ok, and here the code is doing a name-lookup for "currency-name" and
finds this first definitio within the "euro-zone" context.
Thanks a lot for all these very helpful examples. Robert