Shouldn't this trigger a compiler error?

Everything else that doesn't fall into one of the other PB categories.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Shouldn't this trigger a compiler error?

Post by Dude »

I wasn't taking the default value into account. I thought you meant the variable itself wasn't being defined. Sorry!
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 »

Dude wrote:I wasn't taking the default value into account. I thought you meant the variable itself wasn't being defined. Sorry!
Both :wink:
User avatar
mhs
Enthusiast
Enthusiast
Posts: 101
Joined: Thu Jul 02, 2015 4:53 pm
Location: Germany
Contact:

Re: Shouldn't this trigger a compiler error?

Post by mhs »

As you can see, the variable is defined by the compiler as a string:

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()
However the default value is ignored. I think that's a question of design philosophy of the compiler.
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 »

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.
There is a difference between declaration and definition. The declaration is moved to the beginning but of course the definition (= initialization)
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).
Kukulkan wrote:
Shield wrote:Say what? It's not undefined. The "Protected" keyword defines it, just like the manual says.
I didn't say this.
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".
Again, the *declaration* wasn't ignored. It has obviously been declared as mhs showed.
It hasn't been *defined* with your value since that assign statement has never been executed.
-> This is totally to be expected.
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
TI-994A
Addict
Addict
Posts: 2736
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Shouldn't this trigger a compiler error?

Post by TI-994A »

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".
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.

That's the rule in C anyway. :wink:
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 :D
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 »

I just did some tests here to see how PB compares to other compilers:

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);
	}
}
-> Compilation error (line 10, col 21): The name 't' does not exist in the current context

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
-> Compilation error (line 8, col 0): 't' is not declared. It may be inaccessible due to its protection level.

JS

Code: Select all

var z = 0;
if (z == 1) {
  var t = "Hallo";
}
writeln("Out " + t);
-> the variable t is "undefined"

PHP

Code: Select all

$z = 0;
if ($z == 1) {
  $t = "Hallo";
}
echo $t;
-> Notice: Undefined variable: t in [...][...] on line 5

I still think that PB should trigger, at least, some warning (like PHP does).
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 »

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.
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
TI-994A
Addict
Addict
Posts: 2736
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Shouldn't this trigger a compiler error?

Post by TI-994A »

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).
Essentially, this is what PureBasic does to your code:

Code: Select all

Procedure test()
  Protected t.s
  If 1 = 1
    t = "Hallo"
  EndIf
  PrintN(t)
EndProcedure
It declares the variable but does not initialise it with any default value.
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 :D
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

Re: Shouldn't this trigger a compiler error?

Post by HanPBF »

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()
Defined 1 is not in debug output; so starting with "Protected t" the variable is available.
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...
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: Shouldn't this trigger a compiler error?

Post by mariosk8s »

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