Shouldn't this trigger a compiler error?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Shouldn't this trigger a compiler error?

Post by Kukulkan »

Just encountered this issue:

Code: Select all

EnableExplicit

Procedure test()
  If 1=0
    Protected t.s = "Hallo"
  EndIf
  PrintN(t.s)
  
EndProcedure

OpenConsole()
test()
CloseConsole()
I believe the compiler should complain about undefined t.s. Or not? Searched a long time for a bug because it didn't complain.

It should, at least, trigger a warning in such case.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Shouldn't this trigger a compiler error?

Post by Shield »

PB doesn't have scopes other than functions and modules, so it doesn't matter where a variable is defined within a procedure as long as it is defined first. Not a bug but not elegant either.
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
User avatar
skywalk
Addict
Addict
Posts: 4213
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Shouldn't this trigger a compiler error?

Post by skywalk »

haha, if PB had an optimizer, then 'if 1=0' code would be dropped and EnableExplicit would catch the undefined variable.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Shouldn't this trigger a compiler error?

Post by Shield »

No, this would happen at a later stage so the variable would already have been recognized. :)
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: 4781
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Shouldn't this trigger a compiler error?

Post by Little John »

Kukulkan wrote:I believe the compiler should complain about undefined t.s. Or not?
Like Shield, I don't expect the PB compiler to complain in this case.

Do not confuse the situation with the following one:

Code: Select all

  CompilerIf 1=0
    Protected t.s = "Hallo"
  CompilerEndIf
If you change your code like this, then the compiler correctly will complain about undefined variable t.
Kukulkan wrote:Searched a long time for a bug because it didn't complain.

It should, at least, trigger a warning in such case.
When I read code like yours above, my eyes are triggering a warning. ;-) I never write code like that.
In other words, for me personally code is much better readable when all protected variables are defined at the beginning of the respective procedure.
It's that simple! :-)
User avatar
skywalk
Addict
Addict
Posts: 4213
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Shouldn't this trigger a compiler error?

Post by skywalk »

Little John wrote:~ for me personally code is much better readable when all protected variables are defined at the beginning of the respective procedure.
That is being done despite the user's intention with the 'if 1=0'.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4781
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Shouldn't this trigger a compiler error?

Post by Little John »

skywalk wrote:
Little John wrote:~ for me personally code is much better readable when all protected variables are defined at the beginning of the respective procedure.
That is being done despite the user's intention with the 'if 1=0'.
Sorry, I don't understand what your reply means.
Can you please explain a little more elaborately?
User avatar
skywalk
Addict
Addict
Posts: 4213
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Shouldn't this trigger a compiler error?

Post by skywalk »

I seem to recall Fred/freak said all variables within a Procedure are defined despite being defined in a conditional branch. Since the condition is unknown at compile time.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4781
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Shouldn't this trigger a compiler error?

Post by Little John »

skywalk wrote:I seem to recall Fred/freak said all variables within a Procedure are defined despite being defined in a conditional branch. Since the condition is unknown at compile time.
Yes, Kukulkan's If branch is evaluated at run time.
What does happen or does not happen at compile time is controlled by CompilerIf, CompilerSelect, etc.
Of course, Protected is evaluated at compile time.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Shouldn't this trigger a compiler error?

Post by GPI »

My personal opinion:
At least the compiler should output a warning, when a variable definition is inside of a structure like if-endif, while-wend, repeat-until, for-next.
It is a realy bad program style.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Shouldn't this trigger a compiler error?

Post by Shield »

skywalk wrote:I seem to recall Fred/freak said all variables within a Procedure are defined despite being defined in a conditional branch. Since the condition is unknown at compile time.
In PB (IIRC), every declared variable will be moved to the beginning of the procedure by the compiler.
I prefer to define variables when they are used first, but since that doesn't matter in PB I also put them at the very beginning.
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
User avatar
Demivec
Addict
Addict
Posts: 4265
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Shouldn't this trigger a compiler error?

Post by Demivec »

Shield wrote:
skywalk wrote:I seem to recall Fred/freak said all variables within a Procedure are defined despite being defined in a conditional branch. Since the condition is unknown at compile time.
In PB (IIRC), every declared variable will be moved to the beginning of the procedure by the compiler.
I prefer to define variables when they are used first, but since that doesn't matter in PB I also put them at the very beginning.
The memory for the variable is set aside at the start of the procedure but if the variable is also assigned a value as part of the 'Protected' statement the assignment occurs at the place dictated in the code. This was demonstrated in the sample code as well.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Shouldn't this trigger a compiler error?

Post by Kukulkan »

Little John wrote:When I read code like yours above, my eyes are triggering a warning. ;-) I never write code like that.
John, this is to demonstrate the problem only. Obviously, this does not make any sense in real code. The situation for me happened in a very old function that I re-designed (sadly to much copy&paste) so the "Protected" slipped into a bigger If condition. Later I wondered why the variable was empty at some point. I assumed that "EnableExplicit" should warn and the "Protected" statement defined some default initialization value.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Shouldn't this trigger a compiler error?

Post by Dude »

Kukulkan wrote:I believe the compiler should complain about undefined t.s
It's not undefined. The "Protected" keyword defines it, just like the manual says: "When enabled, all the variables which are not explicitly declared with Define, Global, Protected or Static are not accepted and the compiler will raise an error."
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Shouldn't this trigger a compiler error?

Post by Kukulkan »

Shield wrote:In PB (IIRC), every declared variable will be moved to the beginning of the procedure by the compiler.
I believe it is not. In such case, the value of t.s would have been "Hallo". But it is empty. Thus, the whole declaration was ignored.

By the way, the discussion currently ignores the default value I set using "Protected". If I define a variable using "Protected" together with a default value, the first usage of that variable should contain the initialization value. In my example you can see, that also the assignment of the default value is missing.

No matter if this is good practice or not, I agree to GPI that this should trigger at least a warning during compile time.
Shield wrote:Say what? It's not undefined. The "Protected" keyword defines it, just like the manual says.
My declaration of t.s contained a default value. This was ignored by the compiler as during the first usage of the variable it was empty. But this is not what I defined using "Protected".

Sorry, we might have different opinions here. But that's ok :) Fred will have to decide...
Post Reply