Page 1 of 1

Defined(Label, #PB_Label) problem ?

Posted: Tue Jan 15, 2019 12:58 pm
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?

Re: Defined(Label, #PB_Label) problem ?

Posted: Tue Jan 15, 2019 1:07 pm
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()

Re: Defined(Label, #PB_Label) problem ?

Posted: Tue Jan 15, 2019 1:12 pm
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()

Re: Defined(Label, #PB_Label) problem ?

Posted: Tue Jan 15, 2019 6:03 pm
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.

Re: Defined(Label, #PB_Label) problem ?

Posted: Tue Jan 15, 2019 8:10 pm
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.

Re: Defined(Label, #PB_Label) problem ?

Posted: Tue Jan 15, 2019 8:30 pm
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

Re: Defined(Label, #PB_Label) problem ?

Posted: Wed Jan 16, 2019 8:05 pm
by NicTheQuick
I think Defined behaves from the same perspective as Goto would see the label.

Re: Defined(Label, #PB_Label) problem ?

Posted: Wed Jan 16, 2019 8:26 pm
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.