Code: Select all
EnableExplicit
Procedure test()
If 1=0
Protected t.s = "Hallo"
EndIf
PrintN(t.s)
EndProcedure
OpenConsole()
test()
CloseConsole()
It should, at least, trigger a warning in such case.
Code: Select all
EnableExplicit
Procedure test()
If 1=0
Protected t.s = "Hallo"
EndIf
PrintN(t.s)
EndProcedure
OpenConsole()
test()
CloseConsole()
Like Shield, I don't expect the PB compiler to complain in this case.Kukulkan wrote:I believe the compiler should complain about undefined t.s. Or not?
Code: Select all
CompilerIf 1=0
Protected t.s = "Hallo"
CompilerEndIf
When I read code like yours above, my eyes are triggering a warning.Kukulkan wrote:Searched a long time for a bug because it didn't complain.
It should, at least, trigger a warning in such case.
That is being done despite the user's intention with the 'if 1=0'.Little John wrote:~ for me personally code is much better readable when all protected variables are defined at the beginning of the respective procedure.
Sorry, I don't understand what your reply means.skywalk wrote:That is being done despite the user's intention with the 'if 1=0'.Little John wrote:~ for me personally code is much better readable when all protected variables are defined at the beginning of the respective procedure.
Yes, Kukulkan's If branch is evaluated at run time.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.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.
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.Shield wrote:In PB (IIRC), every declared variable will be moved to the beginning of the procedure by the compiler.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.
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.
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.Little John wrote:When I read code like yours above, my eyes are triggering a warning.I never write code like that.
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."Kukulkan wrote:I believe the compiler should complain about undefined t.s
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.Shield wrote:In PB (IIRC), every declared variable will be moved to the beginning of the procedure by the compiler.
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".Shield wrote:Say what? It's not undefined. The "Protected" keyword defines it, just like the manual says.