Shouldn't this trigger a compiler error?
Re: Shouldn't this trigger a compiler error?
I wasn't taking the default value into account. I thought you meant the variable itself wasn't being defined. Sorry!
Re: Shouldn't this trigger a compiler error?
BothDude wrote:I wasn't taking the default value into account. I thought you meant the variable itself wasn't being defined. Sorry!

Re: Shouldn't this trigger a compiler error?
As you can see, the variable is defined by the compiler as a string:
However the default value is ignored. I think that's a question of design philosophy of the compiler.
Code: Select all
EnableExplicit
Procedure test()
If 1=0
Protected t.s = "Hallo"
EndIf
If Defined(t, #PB_Variable) : Debug "Defined" : EndIf
Debug TypeOf(t)
Debug #PB_String
EndProcedure
OpenConsole()
test()
CloseConsole()
Re: Shouldn't this trigger a compiler error?
There is a difference between declaration and definition. The declaration is moved to the beginning but of course the definition (= initialization)Kukulkan wrote: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.
must be kept on the same line, otherwise the program wouldn't be the same (for example in case of a function call to get the first value).
I didn't say this.Kukulkan wrote:Shield wrote:Say what? It's not undefined. The "Protected" keyword defines it, just like the manual says.
Again, the *declaration* wasn't ignored. It has obviously been declared as mhs showed.Kukulkan wrote: 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".
It hasn't been *defined* with your value since that assign statement has never been executed.
-> This is totally to be expected.
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
Re: Shouldn't this trigger a compiler error?
There appears to be some confusion in terms. Whether or not a variable definition is executed, the PureBasic compiler would still automatically declare it. Only when a variable is defined is it instantiated and assigned any default values.Kukulkan wrote: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".
That's the rule in C anyway.

Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Shouldn't this trigger a compiler error?
I just did some tests here to see how PB compares to other compilers:
C#
-> Compilation error (line 10, col 21): The name 't' does not exist in the current context
VB.NET
-> Compilation error (line 8, col 0): 't' is not declared. It may be inaccessible due to its protection level.
JS
-> the variable t is "undefined"
PHP
-> Notice: Undefined variable: t in [...][...] on line 5
I still think that PB should trigger, at least, some warning (like PHP does).
C#
Code: Select all
using System;
public class Program
{
public static void Main()
{
int z = 0;
if (z == 1) {
String t = "Hallo";
}
Console.WriteLine(t);
}
}
VB.NET
Code: Select all
Imports System
Public Module Module1
Public Sub Main()
Dim z As Integer = 0
if z = 1 then
Dim t as string = "Hallo"
end if
Console.WriteLine(t)
End Sub
End Module
JS
Code: Select all
var z = 0;
if (z == 1) {
var t = "Hallo";
}
writeln("Out " + t);
PHP
Code: Select all
$z = 0;
if ($z == 1) {
$t = "Hallo";
}
echo $t;
I still think that PB should trigger, at least, some warning (like PHP does).
Re: Shouldn't this trigger a compiler error?
This is because C# and VB.NET both have a notion of scopes, so t is only valid within the if block.
JavaScript and PHP report the variable as undefined because they are interpreted languages (or more correct,
they are treated as if they were interpreted), which is why the value is undefined (i.e. not initialized), because
the variable has not been defined before its first use. Both languages have no concept of "EnableExplicit".
Edit: the reason why PHP triggers a warning and PB doesn't is because PB knows that the variable exists,
since it has been declared (and found during compilation). PHP doesn't undergo a traditional compilation process,
so it cannot know that the variable exists, it only knows that it hasn't been initialized.
JavaScript and PHP report the variable as undefined because they are interpreted languages (or more correct,
they are treated as if they were interpreted), which is why the value is undefined (i.e. not initialized), because
the variable has not been defined before its first use. Both languages have no concept of "EnableExplicit".
Edit: the reason why PHP triggers a warning and PB doesn't is because PB knows that the variable exists,
since it has been declared (and found during compilation). PHP doesn't undergo a traditional compilation process,
so it cannot know that the variable exists, it only knows that it hasn't been initialized.
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
Re: Shouldn't this trigger a compiler error?
Essentially, this is what PureBasic does to your code:Shield wrote:...the reason why PHP triggers a warning and PB doesn't is because PB knows that the variable exists, since it has been declared (and found during compilation).
Code: Select all
Procedure test()
Protected t.s
If 1 = 1
t = "Hallo"
EndIf
PrintN(t)
EndProcedure
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Shouldn't this trigger a compiler error?
Code: Select all
EnableExplicit
Procedure test()
If Defined(t, #PB_Variable) : Debug "Defined 1" : EndIf
;debug t ; error if uncommented
If 1=0
Protected t.s = "Hallo"
EndIf
If Defined(t, #PB_Variable) : Debug "Defined 2" : EndIf
debug t
Debug TypeOf(t)
Debug #PB_String
EndProcedure
OpenConsole()
test()
CloseConsole()
As there is no local scope and no undefine(t, #PB_Variable) (while the latter in a compiler makes no sense), variables should be declared like in Pascal only at the beginning of a procedure.
If declaration gets out of context then maybe a further splitting of procedure is needed...
- mariosk8s
- Enthusiast
- Posts: 103
- Joined: Wed Apr 06, 2011 11:37 am
- Location: Hüfingen, Germany
- Contact:
Re: Shouldn't this trigger a compiler error?
The issue is not whether the variable is defined or not, but rather whether it is in the expected state.
The problem with this is that it leads to potentially hard to find runtime bugs.
One could argue that it is the programmers fault. I agree. Programmers do screw things up.
That is why one of the jobs of a compiler is lexical validation followed by translation.
This issue clearly falls under lexical validation.
I realize that PB has limited scope support, that's why this would be a feature request.
It would prevent unnecessary runtime bugs.
After all that is the primary reason for EnableExplicit, isn't it?
The problem with this is that it leads to potentially hard to find runtime bugs.
One could argue that it is the programmers fault. I agree. Programmers do screw things up.
That is why one of the jobs of a compiler is lexical validation followed by translation.
This issue clearly falls under lexical validation.
I realize that PB has limited scope support, that's why this would be a feature request.
It would prevent unnecessary runtime bugs.
After all that is the primary reason for EnableExplicit, isn't it?