how is 'set' working ?
[1/2] from: riachtchenko::docutec::de at: 19-Dec-2000 17:55
Hi folks,
i'm just trying some code
from Ingo,
so
>> cursor2: func [
[ commands [block!]
[ /local rules string arg
[ ][
[ string: copy ""
[ rules: [
[ any [
[ set arg string! (append string arg)
[ ]
[ ]
[ parse commands rules
[ print string
[ ]
>> cursor2 ["sascha"]
sascha
>> arg: 4
== 4
>> string: "who"
== "who"
>> set arg string! (append string arg)
** Script Error: set expected word argument of type: any-word block.
** Where: set arg string! (append string arg)
1. What is the difference, as i tried to do the same on the prompt?
>> set 'arg string! (append string arg)
== "whostring"
2. ??
Thanks
Sascha.
[2/2] from: rebol:techscribe at: 19-Dec-2000 11:09
Hi Sasha,
The code fragment you are looking at: "set arg string!" is part of a
parser rule, and therefor it is part of the parse dialect. The parse
dialect includes a word called "set", which is different from the
default "set" function that REBOL normally uses.
As a parse rule "set arg string!" means "match data in the parse input
stream whose datatype is a string!. Once you accomplish that match, then
set
the word "arg" to the beginning of the string you found, and then
do whatever is contained in the following parentheses (i.e. "(append
string arg)").
In contrast, when you enter "set arg string!" at the REBOL prompt you
are not using "set" as above (i.e. as defined in the parse dialect), you
are instead using the REBOL's set function.
The REBOL function set expects a word (or a block containing words), and
a value (or a block of values). The set function associates the word
with the value. Because you are entering the word at the REBOL console,
REBOL will try to evaluate the word arg, before its value is handed over
to the set function. Therefore you get the error message. Before the set
function was called REBOL discovered that the word arg does not evaluate
to anything. Instead you must use the literal word 'arg (as you do in
your second example.) Now REBOL evaluates the literal word 'arg, which
evaluates to the word arg, and that word (arg) is passed to set as its
first argument.
When you enter the expression
>> set 'arg string! (append string arg)
you are setting the word arg to the value string!, which is the string
datatype designator. Later you append arg to a string. Whenever a value
is insert into a string (append uses insert) the value is converted to a
string, if it is not already a string. Try
>> print mold head insert "" string!
You will see that the datatype designator string! is converted to the
string "string". In your expression you are appending arg, which
evaluates to string! to a string, and therefore the string "string"
(after conversion) is appended to the value referenced by the word
string, which was previously set to "who".
Hope this helps,
Elan
riachtchenko wrote: