[REBOL] Re: Context - code included- 2nd version
From: joel:neely:fedex at: 16-Sep-2001 11:16
again, Romano,
Romano Paolo Tenca wrote:
> > Hi, Ladislav, Romano, et al,
>
> Hi, Joe, Ladislav et al,
>
> > Might I suggest that this is not very different from
> >
> > >> foo: load "42" == 42
> > >> type? foo == integer!
> > >> foo: to-decimal "42" == 42
> > >> type? foo == decimal!
> > >> foo: to-word "42" == 42
> > >> type? foo == word!
>
> Strange but understable. Core Guide don't say that "42"
> is an invalid word.
>
Actually, it does... In the on-line version of RCUG (the
quickest one for me to check), section 5.1 "Word Names",
begins with the statements:
Words are composed of alphabetic characters, numbers [sic],
and any of the following characters:
? ! . ' + - * & | = _ ~
A word cannot begin with a number [sic], and there are
also some restrictions on words that could be interpreted
as numbers. For example, -1 and +1 are numbers, not words.
(Note that the document confuses the distinction between
numbers and digits. This, unfortunately, is a warning of
things to come...) It seems clear that "42" will not be
recognized as a word name because it begins with a digit.
> What i don't understand is this: Core guide says:
>
> "Words are not case sensitive and can include hyphens and
> a few other special characters such as +, -, ', *, !, ~,
> &, ., and ?."
> ...
> "The following characters are not allowed in words:
> @#$%^,"
>
Let me try another variation on the analogy. If the sequence
of characters "12.0" (without the quotation marks!) appears
in a script, REBOL will assume it should translate that
sequence of characters into a DECIMAL! value because of the
presence of the dot, even though you can over-rule that
interpretation yourself:
>> glorp: load "12." == 12
>> type? glorp == decimal!
>> glump: to-integer "12." == 12
>> type? glump == integer!
>> equal? glorp glump == true
LOAD uses the standard rules, and therefore creates a DECIMAL!
value. If you intervene, however, you can force the creation
of an INTEGER! value, even though integers normally don't
contain dots in their textual representation.
The statements you quoted above from RCUG are really talking
about the standard rules by which REBOL translates text into
internal values. They don't mean that that you can't override
the normal assumptions and force REBOL to create values (e.g.
words) out of something that doesn't follow the standard rules.
Even though the standard lexical scanning rules for REBOL are
that something starting with an octothorp ("#") will not be
translated into a WORD! value, you can force REBOL to create
such a word, as your example below illustrates:
> >> bind to word! "#" 'system
> == #
> >> #: 3
> == 3
>
We can expand this example of explicit over-riding:
>> trick: reduce [to set-word! "#" 35]
== [#: 35]
>> do trick
== 35
>> get to-word "#"
== 35
>> get '#
** Syntax Error: Invalid word -- '#
** Near: (line 1) get '#
>> get to-lit-word "#"
== 35
As you correctly point out, once the rules are broken, it
can become very confusing to follow what's going on.
> But then when you try to get the value of # Rebol says:
> what dou you want? that is an issue!
>
Because a standard usage of "#" is as the prefix of an
ISSUE! value:
>> type? #1100
== issue!
>> type? #abc
== issue!
(but not the only usage!)
> But what check the interpreter? The datatype of a value
> or its molded value? It is not clear.
>
It's simple, just hard to explain (especially in light of
the overloaded and imprecise terminology of RCUG). Let's
take an example:
>> confusion: reduce [to-set-word "%yikes" 2.54]
== [%yikes: 2.54]
>> do confusion
== 2.54
>> print %yikes
yikes
>> type? %yikes
== file!
What's happening? We forced the creation of a word whose
spelling is not "normal" because it begins with a percent
sign (normally used as a prefix to indicate file names).
Therefore, when we say PRINT %YIKES at the interactive
prompt, REBOL applies the normal rules and translates the
next thing after PRINT into a file name. In order to get
back to the word we created with the non-standard name, we
have to override the rules again...
>> print get to-word "%yikes"
2.54
Hope this helps!
-jn-
--
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com