Comments on: The Bind? function returns the context of a word.
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).
Post a Comment:
You can post a comment here. Keep it on-topic.