Does bind replace the word with a new word or change the existing word
REBOL SCRIPT -
R E B O L 
obj: context [a: 2]
blk: [print a]
b: second blk
print [type? a type? b newline]
get second blk
b == second blk
bind blk in obj 'a
get second blk
b == second blk
1 1 true
1 2 false
I discovered Rebol a few days ago. I found it to be a really interesting language. So I'm a newbie.
When I realized how differently scoping worked in Rebol as compared to other languages, I spent some time trying to understand it and was mostly successful.
Now as per what I understand, a word is a data structure consisting of a symbol which is its spelling and a binding to a context. To get a word's value, the context it's bound to is searched. Depending on what the context is different results are possible.
In the above code the value of b is a word which is the second element of blk.
The second printed set of results is consistent with this.
Now when bind is applied to blk, the binding of a in blk should be changed to the context of the object obj, as the symbol a is present as a variable of the object obj.
The third set of results indicates that the second element of blk is now bound to the context of obj, as it should be.
The value of b is the second element so the binding of the value of b should also change. But the results indicate that it doesn't.
This seems to mean that when the binding of a word is changed, a new word is created which is then used in place of the old one and b, here, continues to refer to the old one.
I don't feel that this is what happens as no one on the internet mentioned it.
Am I missing something obvious ? What's going on ?
posted by: Johann 1-Sep-2021/12:30:31-7:00
Welcome to REBOL.
It's a little of both. Words go into a symbol table that administrates all the words used in the program and the system. It's smart enough to identify all capitalisations of a word as the same symbol.
Now when you use a word, this instance is connected with its capitalisation in that place, and optionally bound to a context. So it is the same symbol, but with some local properties.
The same symbol used somewhere else is the same word, but it can have different capitalisation and context binding.
The local capitalisation can't be changed unless you recreate the word instance, but the binding of a word instance can be changed.
posted by: Kaj 2-Sep-2021/10:26:24-7:00
I didn't have the cycles yesterday to analyse the details of your case. The copying is the other way around than you think. BIND only binds, it does not copy. SET (b: second blk) always copies something. In the case of a scalar, it copies the scalar value. In the case of a series, it only creates a new reference to the series. In the case of a word, it's in the middle as I said yesterday. It copies the local properties of the word, so it creates a new word instance. It's still the same symbol, but the binding is a now independent copy.
If it didn't work like that, the internal implementation would be much more complex, keeping track of many more references. More importantly, the concept of words being able to have different bindings would be defeated.
posted by: Kaj 3-Sep-2021/9:17:13-7:00
Ah, I see what you mean. The statement b: second blk didn't merely reference the word 'a but actually copied the local properties as you said, which includes the binding. I felt I was overlooking something.
Thank you so much, your explanation was very helpful.
And your first post made a lot sense too. I was under the impression that the symbol of a word was stored locally, but I see now that it only needs to be connected with the symbol table.
posted by: Johann 4-Sep-2021/9:05:32-7:00
Just out of curiosity, is there any way to reference a word without creating a copy of its local contents ?
posted by: Johann 4-Sep-2021/10:07:34-7:00
Most functions do not copy a value. In B: SECOND BLK, SECOND just uses the word instance there. It's the implicit SET in B: that copies it to an extra word instance.
Or actually, internally, SECOND does return a copy as its function return value, but it doesn't matter until it's persisted by SET. It is only when the copy is stored in B that there is an opportunity for the local word properties to start to diverge afterwards.
posted by: Kaj 6-Sep-2021/10:45:48-7:00