REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: bind?

bind?  words

Returns the context in which a word is bound.


words [any-word!]

See also:

bind   value?  


The bind? function can tell whether words are bound to a context or not. This tells you if the word has a value related to it.

For example, let's say that obtain the list of the words used by a function (its arguments, refinements, and local variables):

words: words-of :append
[series value /part length /only /dup count]

If you try to get the value of one of those words, you will find that it is not bound. For example, try to get the value of series, the first word:

get first words
** Script error: series word is not bound to a context
** Where: get
** Near: get first words

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:

bind? first words

Now you can write:

value: all [bind? first words get first words]

In REBOL 3, you should know that there are two types of bindings:

absolutea word is bound to a context such as an object, module, or closure. For those, bind? returns the object of a word.
relativea word is bound to a function stack frame. For those, bind? returns true. (It cannot return a context, because for functions contexts are put on the program stack, which is volatile.)

Here is an example of using bind? for object words (absolute bindings):

obj: make object! [
   a: 10
   b: "test"
   c: now
blk: [a b]
bind blk obj
probe reduce blk
[10 "test"]
probe body-of bind? first blk
   a: 10
   b: "test"
   c: 9-Dec-2005/9:03:36-8:00

When used for function words, bind? only tells you if the word is bound:

test: func [arg] [
    if bind? 'arg [print "the word is bound"]
test 123
"the word is bound"

  TOC < Back Next > - WIP Wiki Feedback Admin