REBOL Technologies

The Bind? function returns the context of a word.

Carl Sassenrath, CTO
REBOL Technologies
9-Dec-2005 17:12 GMT

Article #0229
Main page || Index || Prior Article [0228] || Next Article [0230] || Post Comments || Send feedback

REBOL/Core 2.6.2 and /View 1.3.2 include a new function called bind? that is the half-sister of the bind function. Bind? tells you if a word is bound (word has a context) and returns the context (as an object, currently - see note below).

So, what's it good for? Here is an example:

words: first :append
[series value /only]
get first words
** Script Error: series word has no context

The error occurs because the words that represent the arguments of the append are not bound (have no context) within the block. They are unbound. But, with bind?, you can check a word before you get it:

if bind? first words [probe get first words]

Of course, this is more of an expert line of code. But, there is another use for bind? that even beginners will find helpful. Bind? returns an object that tells you about the context of the word provided. For example:

view layout [
    toggle "Test" [
       probe first bind? 'value
    ]
]
[face value]

This example shows you how to obtain information about the context of the toggle action (which is an unnamed function called by the VID toggle style). It shows that there are two local variables, face and value that can be accessed within the action block.

This result comes from the fact that bind? can return the context of a function. This code helps explain it:

amplify: func [value /gain n] [
    probe first bind? 'value
    probe second bind? 'value
    if not gain [n: 10]
    return value * n
]
amplify 10
[value /gain n]
[10 none none]

You can see that the bind? function returns an object that contains the names and values of the arguments and refinements of the function. That information can be quite useful if you are trying to systematically deal with functions that have a lot of refinements.

And finally, as you would expect, bind? can be used for objects as well. Here is an example:

obj: make object! [
    a: 10
    b: "test"
    c: now
]

blk: [a b]
blk: bind blk obj
probe blk
[10 "test"]
print second bind? first blk
a: 10
b: "test"
c: 9-Dec-2005/9:03:36-8:00

Here bind? returns the context of the a word (the object in which it is bound).

Important:

The bind? function currently returns an object datatype. That is not actually correct. It should return a context! datatype (yes, look, it is hidden there in REBOL), but those values have yet to be exported outside the REBOL interpreter itself. So...

To be compatible with future versions of REBOL, use only the first and second functions on the results of bind?. Do not use third or otherwise depend on the result being an object datatype. In the future that will change.

If you try out bind? you will notice the problem if you try to use third on the context of a function (which is not accurately represented within the form of an object as returned by bind?).

Post Comments

Updated 20-Nov-2017   -   Copyright Carl Sassenrath   -   WWW.REBOL.COM   -   Edit   -   Blogger Source Code