Page 1 of 2
EnableExplicit and For loops
Posted: Thu Apr 19, 2012 3:26 pm
by Foz
This is just a little peeve of mine, but I like to work with EnableExplicit on (it cuts down on my errors while coding), but quite often when I have For loops, I want it to automatically declare the variable just for within that loop, for example without EnableExplicit:
Code: Select all
Procedure.i Test(value.i)
For counter.b = 0 To 10
value + counter
Next
Debug counter
ProcedureReturn value
EndProcedure
Debug Test(2)
The for loop declares counter as a byte type, and uses it automatically, but as you can see the variable is persisted after the loop. However, if you add in the EnableExplicit, it will go boom as counter has not been declared.
Now what I would like is the For to automatically declare the variable to be local just to the For loop, so trying to access counter outside of the loop would go boom:
Code: Select all
EnableExplicit
Procedure.i Test(value.i)
For counter.b = 0 To 10 ; <-- No boom
value + counter ; <-- No boom
Next
Debug counter ; <-- BOOM!
ProcedureReturn value
EndProcedure
Debug Test(2)
Good idea or just plain wishful thinking?
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 3:37 pm
by ts-soft
Why not simple use while wend or declare i, j as global for your loops?
And a bytetype for your loop make no sense

Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 3:43 pm
by Foz
I try to avoid as many globals as possible, plus I try to keep all my variables correctly named.
As for the example, it's just that - an example. The logic for correctly defining it works, I'd just like to keep it localised to that for loop.
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 3:49 pm
by Kukulkan
I understand and can follow the argumentation of foz.
What about this syntax:
Code: Select all
Protected value
For Protected counter.b = 0 To 10
value + counter
Next
The drawback is, that Protected is no longer a valid variable name (now a reserved keyword). Or is it already (I don't know)?
Kukulkan
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 4:54 pm
by Derren
Protected is used in procedures.
Code: Select all
Procedure Test()
Protected a.i
a = 1
EndProcedure
Test()
Debug a ; 0 or error when using EnableExplicit
But there's no need for another keyword. There's already
For
I for one vote +1 on this.
Code: Select all
EnableExplicit
Define var = 5
For var=1 to 9
Debug var
Next
Debug var ; 5
ts-soft, how would you use While/Wend without a dedicated variable?
Also, it looks pretty shit compared to For and is more prone to errors, see this:
Code: Select all
var = 5
While var <= 10
var + 2
Debug var
Wend
VS
Code: Select all
For var = 5 to 10 Step 2
debug var
Next
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 5:10 pm
by Little John
Foz wrote:Now what I would like is the For to automatically declare the variable to be local just to the For loop, so trying to access counter outside of the loop would go boom:
Code: Select all
EnableExplicit
Procedure.i Test(value.i)
For counter.b = 0 To 10 ; <-- No boom
value + counter ; <-- No boom
Next
Debug counter ; <-- BOOM!
ProcedureReturn value
EndProcedure
Debug Test(2)
There
are programming languages in which
For loops work exactly this way.
However, while this feature would help us a little to keep our code clean, there is a drawback:
Sometimes we still need the value of the counter outside of the loop, but according to your suggestion it's not possible to access the counter outside the loop.
Regards, Little John
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 5:13 pm
by Shield
+1 on that...and for the record, I'd welcome scope support for all the other statements as well.
Code: Select all
If test
Protected a.i
EndIf
test = a ; << BOOM!
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 5:22 pm
by Little John
Shield wrote:+1 on that...and for the record, I'd welcome scope support for all the other statements as well.
Code: Select all
If test
Protected a.i
EndIf
test = a ; << BOOM!
Allowing the use of
Protected (or another keyword) for this purpose is a good idea IMHO.
But
automatic declaration of the respective variable to be local (as Foz wrote) has its drawbacks.
Regards, Little John
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 5:33 pm
by ts-soft
Derren wrote:But there's no need for another keyword. There's already For
Code: Select all
Global i
Procedure foo()
For i = 1 To 10
Next
EndProcedure
foo()
Debug i
is broken in this way!
Code: Select all
Procedure foo(); this is the best for me ;)
Protected i
For i = 1 To 10
Next
EndProcedure
Procedure foo() ; looks so bad
For Protected i = 1 To 10
Next
EndProcedure
- 1
Re: EnableExplicit and For loops
Posted: Thu Apr 19, 2012 11:28 pm
by Foz
For me, if I
explicitly define the type in the For statement, then that should be the equivalent of a explicitly declared variable.
For example, currently:
Code: Select all
Procedure Foo()
Protected counter.i
For counter = 1 To 10
Next
EndProcedure
However, for a scope based variable just for the For:
Code: Select all
Procedure Foo()
For counter.i = 1 To 10
Next
EndProcedure
It makes it very readable in my mind
Re: EnableExplicit and For loops
Posted: Fri Apr 20, 2012 9:10 am
by xorc1zt
maybe allowing the declaration of scope like C would resolve this
Code: Select all
int main()
{
int r = 45;
{ // open a new scope
int r = 99;
int j = 65;
printf("%i\n", r); // 99
}
printf("%i\n", r); // 45
printf("%i\n", j); // error undeclared identifier
system("PAUSE");
return 0;
}
Re: EnableExplicit and For loops
Posted: Fri Apr 20, 2012 9:19 am
by Foz
Hmmm... Scope...EndScope - that has potential of being used in many other places... just as other forms of loops.
Good idea!
Re: EnableExplicit and For loops
Posted: Fri Apr 20, 2012 9:41 am
by Shield
It would be more useful to add scope support for loops, if / else, select, etc.
I work quite a bit with C styled languages but I never used an individual { } scope.
And in my opinion it's unnecessary as it should just be replaced by a function call.
Oh...and I think it's just tedious to have to write Scope / EndScope all the time...
Code: Select all
For i = 0 To 100
Scope
; Code
EndScope
Next
Re: EnableExplicit and For loops
Posted: Fri Apr 20, 2012 7:14 pm
by Zach
I think that's what the curly braces are intended to be for (or are reserved in PB already? Can't remember having to use them)
Re: EnableExplicit and For loops
Posted: Sat Apr 21, 2012 11:59 am
by Little John
Foz wrote:For me, if I
explicitly define the type in the For statement, then that should be the equivalent of a explicitly declared variable.
[...]
However, for a scope based variable just for the For:
Code: Select all
Procedure Foo()
For counter.i = 1 To 10
Next
EndProcedure
It makes it very readable in my mind
Just appending a type identifier such as
.i to a variable name (without using
Define, Global, Protected or
Static) is currently not considered expilict declaration of a variable in PB. If your suggestion would be implemented in this way, it would change the meaning of
EnableExplicit. That would cause unnecessary confusion.
Regards, Little John