Undocumented: Index value local to FOR loop
[1/5] from: gchillemi::aliceposta::it at: 23-Mar-2004 15:50
I have found this undocumentend behaviour:
Try this script:
rebol [
Author: "Giuseppe Chillemi"
Tittle: "A little attemp"
]
esterno: does [
print [indice]
]
for indice 1 10 1 [
esterno
]
Halt
Rebol tell me that "indice" is not initalized but it is ! This word seems to
remain local to the for loop.
I suppose the mailing list already know this. I write it for just
confirmation from the community.
Giuseppe Chillemi
[2/5] from: greggirwin:mindspring at: 23-Mar-2004 11:04
Hi Giuseppe,
GC> I have found this undocumentend behaviour:
GC> for indice 1 10 1 [esterno]
GC> Rebol tell me that "indice" is not initalized but it is ! This word seems to
GC> remain local to the for loop.
Yes, FOREACH and REPEAT work the same way. FOR is a mezzanine, so you
can see how it works--though it is a bit advanced if you happen to be
new to REBOL.
Look at the core docs (e.g. core.pdf) at how you can define
literal-arguments and get-arguments for functions. REBOL uses
'definitional scoping', which may seem a bit confusing at first when
used with literal arguments.
>> fn: func ['word] [print word word: 10 print word]
>> fn hello
hello
10
>> hello
** Script Error: hello has no value
** Near: hello
HTH!
-- Gregg
[3/5] from: nitsch-lists:netcologne at: 24-Mar-2004 1:42
Hi Giuseppe,
On Dienstag, 23. M=E4rz 2004 15:50, Giuseppe Chillemi wrote:
> I have found this undocumentend behaviour:
> Try this script:
rebol []
esterno: does [
print [indice]
]
for indice 1 10 1 [
esterno
]
Halt
> Rebol tell me that "indice" is not initalized but it is ! This word seems
> to remain local to the for loop.
I quickly step in, even if Greg described the technical:
some loops make locals for you implicitely.
so
for indice 1 10 1 [ esterno ]
is the same as
use[indice][
for indice 1 10 1 [ esterno ]
]
so the global is not changed.
and your 'esterno uses the global.
thats handy because i usually use the same varname for loopvars, like 'i, and
have not to declare it as local.
further note: its not consistent, 'forall, 'forskip do not do this.
has convenience-reasons too IMHO. Nested foralls, before block-parser was
there.
> I suppose the mailing list already know this. I write it for just
> confirmation from the community.
>
> Giuseppe Chillemi
-volker
[4/5] from: lmecir:mbox:vol:cz at: 24-Mar-2004 5:37
Gregg Irwin napsal(a):
>...REBOL uses
>'definitional scoping', which may seem a bit confusing at first when
<<quoted lines omitted: 11>>
>** Near: hello
>HTH!
The above code doesn't explain what is going on.
esterno: does [
print [indice]
]
use [indice] [
indice: 25
esterno
]
here you can see, that esterno doesn't use initialized local word,
because it uses the uninitialized global.
-L
[5/5] from: greggirwin:mindspring at: 24-Mar-2004 8:15
Ladislav and Volker,
Good thing you guys were here to catch me! I had a definite "huh?"
moment when I saw Volker's message, then saw that I had a
completely different train of thought when I read the original
message. My brain said that the error was from global use of indice after
the loop was done, not esterno using indice in the loop. Not sure how
I did that, but thanks again for catching me.
--Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted