bind
[1/6] from: gchiu::compkarori::co::nz at: 19-Oct-2001 8:21
Would someone be able to explain in english what 'bind does.
I've read the explanation in the Rebol word browser and am
no clearer than before :(
--
Graham Chiu
[2/6] from: joel:neely:fedex at: 18-Oct-2001 13:59
Hi, Graham,
Graham Chiu wrote:
> Would someone be able to explain in english what 'bind does.
> I've read the explanation in the Rebol word browser and am
> no clearer than before :(
>
I'm not sure about my American (I don't speak English... ;-) ,
but please let me know if this is of any use to you. I'll be glad
to try to answer any questions it raises.
-jn-
A REBOL word is just a name or symbol. Its value comes from the
context that gives it meaning. Words in different contexts can
have totally unrelated values, even when the words' names are spelled
the same.
Consider this example:
>> glorp: "I'm global!"
== "I'm global!"
>> wordblock: [glorp]
== [glorp]
>> some-fun: func [b [block!] /local glorp] [
[ glorp: "I'm local!"
[ print [glorp]
[ print b
[ ]
>> some-fun wordblock
I'm local!
I'm global!
The word named "glorp" in the block is the externally-defined word.
That fact doesn't change just because the block is handed in to the
function. Therefore the second line of output is still referring
to the value of the global "glorp" and not the local one.
The role of BIND is to change the context of words in its first
argument (if possible) to be the context of its second (sample)
argument. Therefore, we can do this:
>> glorp: "I'm global!"
== "I'm global!"
>> glunk: "I'm global, too!"
== "I'm global, too!"
>> wordblock: [glorp glunk]
== [glorp glunk]
>> some-fun: func [b [block!] /local glorp foo] [
[ glorp: "I'm local!"
[ foo: 42
[ print [glorp]
[ print b
[ bind b 'foo
[ print b
[ ]
Similar exercise, but I added another global word to the block.
In addition, I added an irrelevant local named FOO, just to show
that the second argument to BIND is simply a *sample* word which
indicates which context to use -- you don't need to worry about
making names match.
>> print wordblock
I'm global! I'm global, too!
Just what we expect.
>> some-fun wordblock
I'm local!
I'm global! I'm global, too!
I'm local! I'm global, too!
Notice that after BIND does its thing, the first word in the block
has been changed (bound) to the function's context. That's why the
last output line is different from the second.
>> print wordblock
I'm local! I'm global, too!
Notice too, that the change is permanent. Now the "glorp" in the
first position of WORDBLOCK is the one in the function's context
and not the global one.
If this kind of aliasing is a problem, you could rewrite the demo
function as:
>> some-fun: func [b [block!] /local glorp foo] [
[ glorp: "I'm local!"
[ foo: 42
[ print [glorp]
[ print b
[ b: bind copy b 'foo
[ print b
[ ]
(We also have to re-create WORDBLOCK...)
>> wordblock: [glorp glunk]
== [glorp glunk]
Now we can do this:
>> some-fun wordblock
I'm local!
I'm global! I'm global, too!
I'm local! I'm global, too!
>> print wordblock
I'm global! I'm global, too!
Since SOME-FUN modified a copy of the argument, the "glorp" in the
original WORDBLOCK is not rebound.
Hope this helps!
-jn-
--
This sentence contradicts itself -- no actually it doesn't.
-- Doug Hofstadter
joel<dot>neely<at>fedex<dot>com
[3/6] from: ryanc:iesco-dms at: 18-Oct-2001 12:30
Bind just tells rebol where to look for the definitions of words
in a block when they evaluated by the interpreter.
examples:
>> words: to-block {copy "test"}
== [copy "test"]
>> do words ; the words are not bound to any meanings!
** Script Error: copy is not defined in this context
** Where: do-boot
** Near: copy "test"
>> do bind words in system/words 'self
== "test"
>> do words ; bind is persistent too.
== "test"
>> words: to-block {copy "test"}
== [copy "test"]
>> meanings: context [copy: :alert] ; context is the same as
make object!
>> bind words in meanings 'self
== [copy "test"]
>> do words ; shows an alert box saying "test".
== true
>> ; Same as...
>> meanings/copy "test"
== true
>>
Graham Chiu wrote:
> Would someone be able to explain in english what 'bind does.
> I've read the explanation in the Rebol word browser and am
<<quoted lines omitted: 5>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Ryan Cole
Programmer Analyst
www.iesco-dms.com
707-468-5400
[4/6] from: gchiu:compkarori at: 19-Oct-2001 13:22
Hi Joel,
> >
>
> I'm not sure about my American (I don't speak English...
> ;-) ,
By George, I think I've got it!
--
Graham Chiu
[5/6] from: gchiu:compkarori at: 19-Oct-2001 13:26
On Thu, 18 Oct 2001 12:30:40 -0700
Ryan Cole <[ryanc--iesco-dms--com]> wrote:
> Bind just tells rebol where to look for the definitions
> of words
> in a block when they evaluated by the interpreter.
>
Thanks, Ryan. I understand that even if I found your
examples hard to grok :)
--
Graham Chiu
[6/6] from: lmecir:mbox:vol:cz at: 19-Oct-2001 2:10
Hi Graham,
I suggest you to try to read http://www.sweb.cz/LMecir/contexts.html or to
look at my Rebsite for Contexts.
Cheers
Ladislav
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted