EnableExplicit and For loops

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

EnableExplicit and For loops

Post 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?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: EnableExplicit and For loops

Post 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 :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: EnableExplicit and For loops

Post 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.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: EnableExplicit and For loops

Post 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
User avatar
Derren
Enthusiast
Enthusiast
Posts: 316
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: EnableExplicit and For loops

Post 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
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: EnableExplicit and For loops

Post 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
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: EnableExplicit and For loops

Post 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!

Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: EnableExplicit and For loops

Post 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
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: EnableExplicit and For loops

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: EnableExplicit and For loops

Post 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
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: EnableExplicit and For loops

Post 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;
}
 
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: EnableExplicit and For loops

Post by Foz »

Hmmm... Scope...EndScope - that has potential of being used in many other places... just as other forms of loops.

Good idea!
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: EnableExplicit and For loops

Post 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
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: EnableExplicit and For loops

Post 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)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: EnableExplicit and For loops

Post 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
Post Reply