3

In experimenting with words in Rebol 3, I ran into the following error.

>> set to lit-word! "g" 4
** Script error: 'g word is not bound to a context
** Where: set
** Near: set to lit-word! "g" 4

This seems pretty tricky because of the following results:

>> to lit-word! "g"
== 'g
>> set 'g 4
== 4

I was wondering how a word cannot be bound to a context when it looks identical to the above...

kealist
  • 1,669
  • 12
  • 26
  • Any particular reason in this case why you use `to lit-word!` and not just `to word!`? – rgchris Aug 24 '13 at 23:36
  • Generally I use `to lit-word!` because the first time a value is set, it has no value, and using the `word!` gives an error: `>> set o 4 ** Script error: o has no value` Does that make sense here? – kealist Aug 25 '13 at 03:34
  • Using `to lit-word!` won't help at all with that, since `set` treats the word the same regardless of its type. In the second case, the `'g` gets evaluated and converted to a `word!` value before it is set, but its type is irrelevant. The main difference is that the `'g` is loaded by `load` and bound to the user context before anything in that line of code is run. – BrianH Aug 27 '13 at 22:45

1 Answers1

4

In Rebol 3 there is certain behavior of the console and scripts that is important to understand:

Anything you type is loaded by Rebol. When it is loaded, it is put in a context.

If I type:

b: 4
set 'b 5

There is an existing word b or 'b without any of the code/data being evaluated, which is put in the system/contexts/user context, so it has binding to that context.

>> bound? 'b
== make object! [
    system: make object! [
        version: 2.101.0.3.1
        build: 31-May-2013/18:34:38
        platform: [
            Windows win32-x86
        ]
        product: 'saphir-view
        license: {Copyright 2012 REBOL Technologies
            REBOL is a trademark of REBOL Technologies
            Licensed under the Apache License, Version 2.0.
            See: http://www.apache.org/licenses/LICENSE-2.0
        }
        catalog: make object! [
            datatypes: [end! unset! none! logic! integer! decimal! percent! mo...

And to show this is same context:

>> same? (bound? 'b) system/contexts/user
== true

However, when you type to-word "b", all that load sees is a word to-word and a string. So in this case load adds the to-word word to system/contexts/user but nothing happens with binding b because it hasn't been loaded.

>> bound? to word! "b"
== none

Moreover, to word! (or to lit-word!, etc.) when evaluated does not bind anything. That binding must be done manually.

See How are words bound within a Rebol module? for more information

Community
  • 1
  • 1
kealist
  • 1,669
  • 12
  • 26
  • I'd speculate that LOAD has such loaded behaviour as part of LOAD's purpose is to prepare code for evaluation (DO), and that DO needs a default context (USER) as space to maneuver. It follows that the most elemental method of creating a WORD! (TO WORD!) does not have that responsibility nor should make the assumption that it belongs to any particular context—a blank canvas, if you will. LOAD is a mezzanine in Rebol 3 and is replaceable if its aggressive WORD! assimilation proves malign (TRANSCODE being the core arbiter of turning streams into values). – rgchris Aug 24 '13 at 23:44
  • It's generally a bad idea to replace `load` in R3 just because it binds words, because it also does a lot of other useful stuff. If you want the other features of `load` without the word binding, use its `/type 'unbound` option to turn off the binding. There are already other low-level functions that do less stuff, like `to` or `transcode`. – BrianH Aug 27 '13 at 22:50