Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind local context block to global context in Rebol2?

As I understand it, you are supposed to be able to bind any block to any context. In particular you can bind a global context block to a local context:

>> a: context [
    print: does [rebol/words/print "yeah!"]
    f: func[b] [do bind b 'print]
]

>> a/f [print "hello"]
yeah!
== "hello"

So it must be possible to bind a local context block to the global context too? But my attempts were unsuccessful:

>> b: context [
    b: [print "hello"]
    print: does [rebol/words/print "yeah!"]
    f: func[] [do bind b 'system]
]

>> b/b
== [print "hello"]

>> do b/b
yeah!
== "hello"

>> b/f
hello

>> do b/b
hello

It seems I made it but:

>> equal? bind? 'system bind? in b 'b
== false

>> same? bind? in b 'f bind? in b 'b
== true

What is my error here?

like image 932
Pep Avatar asked Feb 14 '23 16:02

Pep


2 Answers

You are binding the words in the block assigned to b/b, you aren't binding the word b itself.

>> equal? bind? 'system bind? in b 'b
== false

This compares two objects, the first is the one that 'system is bound to, and the second is the one that in b 'b is bound to (the top-level b object).

The thing is that blocks aren't really bound, the words in the block are bound. The blocks themselves don't have any bindings, not even as a concept. Also, the block that is assigned to b/b is just a value that happens to be assigned to b/b, it's not the word 'b.

This comparison should work:

>> equal? bind? 'system bind? first get in b 'b
== true

What you are comparing with this is the binding of the first word in the block assigned to b/b, which is that print that you bound earlier. That word is what you changed the binding of in b/f.

like image 156
BrianH Avatar answered Mar 14 '23 19:03

BrianH


The binding information is carried by words, not blocks. When a block is rebound, the binding process will affect the words contained in the block. So your b/b block (and not the in b 'b word) has been rebound correctly, but your check is wrong. You need to retrieve the context from one of the rebound words inside the block, as in:

>> equal? bind? 'system bind? b/b/1
== true
like image 22
DocKimbel Avatar answered Mar 14 '23 20:03

DocKimbel