Defined(Label, #PB_Label) problem ?

Just starting out? Need help? Post your questions and find answers here.
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

Defined(Label, #PB_Label) problem ?

Post by benmalartre »

Here is the problem I'm facing:

I want to check the existence of a label when creating an object
while the label is defined inside the module itself, it is not inside a procedure of the module
(in fact it is , I can assign it with ?Label, but the function Defined(Label, #PB_Label) says it's not)

I tested on both Windows and MacOS with the same result
here is some test code:

Code: Select all

DeclareModule Object
  Declare Test()
  
  DataSection
    ObjectVT:
  EndDataSection

  Macro INI(obj)
    CompilerIf Defined(obj#VT, #PB_Label)
     Debug "LABEL FOUND"
    CompilerElse
      Debug "LABEL NOT FOUND"
    CompilerEndIf
  EndMacro  
EndDeclareModule

Module Object
  INI(Object)
  Debug ?ObjectVT
  Procedure Test()
    INI(Object)
    Debug ?ObjectVT
  EndProcedure
  
EndModule

Object::Test()

which says:
LABEL FOUND
5368956240
LABEL NOT FOUND
5368956240
Is it a bug or normal behavior?
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Defined(Label, #PB_Label) problem ?

Post by NicTheQuick »

It is possible to define local and global labels. In fact each defined label lives in the scope of a procedure, a module or even global. Maybe there is some weirdness going on with the Defined() command here.
Because it is not possible to use Goto with a global label inside of a procedure, it should also not be possible to use ?label inside a procedure. But in fact it is possible. Without this possibility a lot of OOP like modules out there would not work anymore.

Code: Select all

Goto error
Debug "1 never"
error:
Debug "1 always " + StrU(?error, #PB_Quad)

Procedure a()
	Goto error
	Debug "2 never"
	error:
	Debug "2 always " + StrU(?error, #PB_Quad)
EndProcedure

Procedure b()
	Goto error
	Debug "3 never"
	error:
	Debug "3 always " + StrU(?error, #PB_Quad)
EndProcedure

Procedure c()
	Debug "4 what? " + StrU(?error, #PB_Quad)
EndProcedure

a()
b()
c()
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Defined(Label, #PB_Label) problem ?

Post by STARGÅTE »

It seems like it's a problem in Procedures:

Code: Select all

Procedure Test()
  Debug Defined(Test, #PB_Label)
  Debug ?Test
  DataSection
  	Test:
  EndDataSection
EndProcedure

Test()
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Defined(Label, #PB_Label) problem ?

Post by Little John »

Original code simplified (still doesn't work as expected (with PB 5.70 on Windows 10)):

Code: Select all

DataSection
   ObjectVT:
EndDataSection

Debug Defined(ObjectVT, #PB_Label)
Debug ?ObjectVT

Procedure Test()
   Debug Defined(ObjectVT, #PB_Label)
   Debug ?ObjectVT
EndProcedure

Test()
Since

Code: Select all

?ObjectVT
works properly outside and inside the procedure, but

Code: Select all

Defined()
only works properly outside the procedure, I think it's a bug with Defined() in Procedures.

If the label were not visible inside the procedure, then using

Code: Select all

Debug ?ObjectVT
inside the procedure would raise an error.
Last edited by Little John on Tue Jan 15, 2019 10:12 pm, edited 1 time in total.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Defined(Label, #PB_Label) problem ?

Post by Josh »

Defined only returns #True if the label was defined in the same scope and before the Defined command. This is all correct.

Since version 5.10 labels are local. I think it was a compromise to get the address of a label outside the scope to keep old code executable. Maybe it would have been enough if labels in a DataSection were global, but I don't see any problem, as it is now.
sorry for my bad english
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

Re: Defined(Label, #PB_Label) problem ?

Post by benmalartre »

Thanks all to have a look
I avoid the problem by declaring empty labels and not use Defined(Label, #PB_Label) inside of my procs
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Defined(Label, #PB_Label) problem ?

Post by NicTheQuick »

I think Defined behaves from the same perspective as Goto would see the label.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Defined(Label, #PB_Label) problem ?

Post by Josh »

NicTheQuick wrote:I think Defined behaves from the same perspective as Goto would see the label.
No. Goto can be placed before the label, Defined would return a #False at the same position.
sorry for my bad english
Post Reply