metaeducation / ren-c

Library for embedding a Rebol interpreter into C codebases
GNU Lesser General Public License v3.0
126 stars 27 forks source link

Cannot interrogate bindings #1159

Closed jjsullivan5196 closed 3 months ago

jjsullivan5196 commented 3 months ago

In the r3 console on https://github.com/metaeducation/ren-c/commit/70d066816ad705ca489c79b3aa9609e777b7b9cf

>> foo: 5
== 5

>> set? 'foo
(i) Info: use WHY for error information
** Script Error: foo word is not bound to a context
** Where: get set? console
** Near: [*** trash? get/any var **]
** Line: 3795

>> get/any 'foo
** Script Error: foo word is not bound to a context
** Where: get console
** Near: [get/any 'foo **]
** Line: 1

>> unset 'foo
** Script Error: foo word is not bound to a context
** Where: unset console
** Near: [unset 'foo **]
** Line: 1

>> foo
== 5

try doesn't fix it either

>> try get/any 'foo
(i) Info: use WHY for error information
** Script Error: foo word is not bound to a context
** Where: get console
** Near: [try get/any 'foo **]
** Line: 1

>> 

trap is the same

>> trap [get/any 'foo]
** Script Error: foo word is not bound to a context
** Where: get entrap trap console
** Near: [get/any 'foo **]
** Line: 1
hostilefork commented 3 months ago

Binding in Ren-C is radically different...

All source material is unbound by default, and binding propagates as a wave. Under EVAL, a plain WORD! will pick up the binding that is propagating... so will a SET-WORD!. But all quoted material merely drops the quote under evaluation, and does not add the binding.

Blocks act in a similar way. So a block does "evaluate", it just evaluates to a bound version of itself...if it doesn't already have a binding. Given that universal rule about quoted material not affecting binding, it means [print "Hi"] and '[print "Hi"] are different:

>> code: [print "Hi"]
== [print "Hi"]

>> eval code
"Hi"

>> code: '[print "Hi"]
== [print "Hi"]

>> eval code
** Script Error: print word is not bound to a context

If you want a variant of "quoting" that can be used with evaluation, you need to use a VAR-WORD!

>> foo: 10
== 10

>> $foo
== foo

>> set? $foo
== ~true~  ; anti

>> get $foo
== 10

The thought process that went into designing this is laid out in the post "Rebol and Scopes: Well, Why Not?"

https://forum.rebol.info/t/rebol-and-scopes-well-why-not/1751

It's a time of transition as this new way of thinking takes shape! But it's much better than historical Rebol's chaotic "painting bindings on everything by deep walks", because that generated tons of meaningless bindings.

jjsullivan5196 commented 3 months ago

Thanks for the clarification @hostilefork