Generic For variables

Find quick help here to get you started with Hollywood
Post Reply
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Generic For variables

Post by lazi »

From the generic For description:

For <var1> [, <var2>, ...] In <expr> [Do <stat>] or [<loop-block> Next]

The generic version of the For statement is different from the other two
versions through the fact that it calls a user-defined function to retrieve
the values for each iteration. This fact makes the generic For statement
suitable for a wide variety of purposes.

Is the var1, var2... are always local variables inside the loop?
If I understand correctly those variables need no set to local and they are not write in the variables with the same name.
So they are not really variables. I can use the same variable names outside the loop without confusing anything but the one who want to read the source :)
Is that correct?
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Generic For variables

Post by airsoftsoftwair »

lazi wrote: Sun Jan 24, 2021 4:29 pm Is the var1, var2... are always local variables inside the loop?
Yes.
lazi wrote: Sun Jan 24, 2021 4:29 pm If I understand correctly those variables need no set to local and they are not write in the variables with the same name.
So they are not really variables. I can use the same variable names outside the loop without confusing anything but the one who want to read the source :) Is that correct?
Yes. Those variables are read-only variables. They are local to the respective for loop and you may only read their value. You must not write to them. And they won't clash with variables that have the same name outside the for loop.
User avatar
mrupp
Posts: 147
Joined: Sun Jan 31, 2021 7:44 pm
Location: Switzerland
Contact:

Re: Generic For variables

Post by mrupp »

I was wondering, too, if for-in-variables need to be declared local or not.
So this is same, correct?

Code: Select all

For key, paramValue In Pairs(parameters)
	<do something>
Next

Code: Select all

Local key, paramValue
For key, paramValue In Pairs(parameters)
	<do something>
Next
No need to declare the variables local first, did I get this right?
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Generic For variables

Post by Bugala »

airsoftsoftwair wrote: Thu Jan 28, 2021 10:31 pm
Yes. Those variables are read-only variables. They are local to the respective for loop and you may only read their value. You must not write to them. And they won't clash with variables that have the same name outside the for loop.
Either I don't understand what you say, or they are not behaving accordingly, I have bumped into this problem accidentally before:

Code: Select all

n = 5
For n = 1 To 3
	DebugPrint(n)
Next
DebugPrint("after: "..n)
will result in printing:
1
2
3
after: 4

If they are local to for loop only, then last debugprint should to my understanding print
after: 5
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Generic For variables

Post by SamuraiCrow »

The iterator form of the For loop has internal variables. The other form only does if you add the "local" keyword.

Code: Select all

For key, value In Pairs(var)
has "key" and "value" internally only.

Code: Select all

For Local count=1 to 5
has "count" internally.
I'm on registered MorphOS using FlowStudio.
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Generic For variables

Post by airsoftsoftwair »

mrupp wrote: Sun Feb 07, 2021 11:41 am No need to declare the variables local first, did I get this right?
Yup. They are by definition always local. Only normal for loops have two different modes, local and non-local, and thus need specification what kind is desired. Generic for loops always use local variables.
Post Reply