REBOL 中 Bind 的理解

Rebol文档中的Bind的定义如下:

 

Bind words to a special context.

 

Binds meaning to words in a block. That is, it gives words a context in which they can be interpreted. This allows blocks to be exchanged between different contexts, which permits their words to be understood. For instance a function may want to treat words in a global database as being local to that function.

The second argument to BIND is a word from the context in which the block is to be bound. Normally, this is a word from the local context (e.g. one of the function arguments), but it can be a word from any context within the system.

BIND will modify the block it is given. To avoid that, use the /COPY refinement. It will create a new block that is returned as the result.

 

我个人的理解是:Rebol变量也就是word的存取范围是词法范围,而不是动态范围。所以当一个Block被传递到另外的context比如某个函数中时,evaluate这个block时,block中的word就有可能没有绑定的值。这是就需要使用Bind函数了。

bind someblock ‘someword,就可以将调用这个bind的context也一般也就是someword所在的context合并到someblock的context中(而不是替换someblock的context),此时引用someblock中someword时,rebol就会到定义someword的函数中寻找它的值。

 

类似的情况在clojure也存在。clojure使用binding来产生一个动态绑定。

你可能感兴趣的:(bind)