Protected 'already declared'

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Protected 'already declared'

Post by Rinzwind »

Inconsistent behavior.

Consider:

Code: Select all

EnableExplicit

Global test
test = 1
;.
;.lot of other code
;.
Global test
Debug test

Procedure proc()
  Protected test
;test used
;.
;.
;.
;a lot of other code
;
; again needed (possibly in other if branche)
  Protected test
EndProcedure
For Globals it is allowed to declare a variable multiple times, for Protected not for some reason? It would be very helpful for situations when you want to keep variables near the area where they are used, instead of all dumped at the top of procedure (sometimes need a variable in more than one area, so helpful if it allows multiple declares).

With Define multiple declares are also allowed, but that's not a local variable when a Global one already exists... and hence confusing and source of bugs.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Protected 'already declared'

Post by #NULL »

From my notes:
- Global does not complain about redeclaration as long as the type does not change.
- Define does not complain about redeclaration but gives error for different implicit type
- Protected does not allow redeclaration at all

Code: Select all

Debug "----------------------------- 13:"

; Global does not complain about redeclaration as long as the type does not change. That includes
; the case where the second declaration does not use any type and intends to use the default type

Global g13.a = 255           ; somewhere
; ...
Global g13                   ; assume integer and zero-initialization?
g13 + 1
Debug g13                    ; 0

Procedure p13()
  Global g13
  g13 + 1
  Debug g13                  ; 1
EndProcedure
p13()

Debug "----------------------------- 14:"

; Define does not complain about redeclaration but gives error for different implicit type

Define d14 = 100             ; somewhere
; ...
Define d14                   ; assume zero-initialization?
d14 + 1
Debug d14                    ; 101

Define d14b.a
;Define d14b                 ; error: Variable already declared with another type

Procedure p14()
  Define d14 = 200
  ; ...
  Define d14                 ; assume zero-initialization?
  d14 + 1
  Debug d14                  ; 201
  
  Define d14_.a = 200
  ;Define d14_               ; error: Variable already declared with another type
EndProcedure
p14()

Debug "----------------------------- 15:"

; Protected does not allow redeclaration at all

Procedure p15()
  Protected d15
  ;Protected d15             ; error: Local variable already declared
EndProcedure
I think it would be good to at least
- disallow the declaration with different [implicit] type for Global
- disallow redeclaration + initialization/assignment for variables that are already declared.
nsstudios
Enthusiast
Enthusiast
Posts: 275
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: Protected 'already declared'

Post by nsstudios »

Ran into this while trying to declare a protected var depending on the if statement.
I believe it's an incorrect behavior for a variable to be created in this case:

Code: Select all

EnableExplicit
Procedure test(a=0)
If a
Protected b$="something";should never be executed
Else
Debug Defined(b$, #PB_Variable); it's defined
Debug b$; what's more, it's empty
;Protected b$="something else"; commented because it would think it's defined already
EndIf
EndProcedure
test()
:?:
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Protected 'already declared'

Post by Little John »

nsstudios wrote:Ran into this while trying to declare a protected var depending on the if statement.
That doesn't make sense because PureBasic is not an interpreted language.
Protected is considered at compile time, but If is executed at runtime.
For what you want to achieve, you'd have to use CompilerIf instead of If.
Last edited by Little John on Sat Jan 25, 2020 2:37 pm, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Protected 'already declared'

Post by mk-soft »

With Protected the variable is defined at compiler time the memory space on the stack, not at runtime by the program
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
nsstudios
Enthusiast
Enthusiast
Posts: 275
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: Protected 'already declared'

Post by nsstudios »

Thanks @Little John   and @mk-soft, my bad.
:oops:
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Protected 'already declared'

Post by mk-soft »

Only works with macros and separate procedures.

Example

Code: Select all

EnableExplicit

Procedure test1()
  Protected b$="something"
  Debug b$
EndProcedure

Procedure test2()
  Protected c=100
  Debug c
EndProcedure

Macro test(state)
  CompilerIf state
    test1()
  CompilerElse
    test2()
  CompilerEndIf
EndMacro

test(#False)
test(#True)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Protected 'already declared'

Post by Josh »

@nsstudios
I would recommend you to write a clean code and define all your variables at the top of the procedure. Then your problem wouldn't even arise.

Code: Select all

EnableExplicit

Procedure test(a=0)
  Protected b$

  If a
    b$="something";should never be executed
  Else
    Debug Defined(b$, #PB_Variable); it's defined
    Debug b$; what's more, it's empty
    b$="something else"; commented because it would think it's defined already
  EndIf

EndProcedure

test()
@Little John
The CompilerIf will not work because nsstudios has made this dependent on a variable.


@mk-soft
I don't understand the need to philosophize about compiletime, runtime and stack in this case. I also don't understand the macro, because when I 'unpack' your macro, there is nothing behind it.
sorry for my bad english
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Protected 'already declared'

Post by Demivec »

@mk-soft
I don't understand the need to philosophize about compiletime, runtime and stack in this case. I also don't understand the macro, because when I 'unpack' your macro, there is nothing behind it.[/quote]

@Josh: There is a need to distiguish compiletime and runtime here.

At compiletime all protected variables are have spread e set aside on the stack. At runtime only those declarations that are executed will have their assignments take place.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Protected 'already declared'

Post by Josh »

@Demivec
No need. There is no need to torture a newcomer with any backgrounds that are absolutely not relevant to his problem. nsstudios just wrote a bad code (solution see code in my last posting).
sorry for my bad english
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Protected 'already declared'

Post by Little John »

Josh wrote: @Little John
The CompilerIf will not work because nsstudios has made this dependent on a variable.
I made a general remark, and did not refer to any details of nsstudios' variables etc.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Protected 'already declared'

Post by Demivec »

Josh wrote:@Demivec
No need. There is no need to torture a newcomer with any backgrounds that are absolutely not relevant to his problem. nsstudios just wrote a bad code (solution see code in my last posting).
@Josh: You speak the truth with regard to the solution and to some degree also newcomers. I think it is also important for the other details to be included in the ongoing discussion for the general benefit of others researching ther own issues in the future.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Protected 'already declared'

Post by mk-soft »

@Demivec

I also know that Macro makes little sense.
Just wanted to show that it only works with Macros(Parameter) and CompilerIf.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Protected 'already declared'

Post by Little John »

BTW, in the first post of this thread it reads:
Rinzwind wrote:

Code: Select all

; [...]

Procedure proc()
  Protected test
;test used
;.
;.
;.
;a lot of other code
;
; again needed (possibly in other if branche)
  Protected test
EndProcedure
[...] (sometimes need a variable in more than one area, so helpful if it allows multiple declares).
Declaring a variable and using it are two different things. There is no need to declare a variable each time it is used.
As already has been written here, just declare all Protected variables at the beginning of a procedure, and you are done. This way, code which is evaluated at compile time is separated from code which is evaluated at runtime. That's what I consider clean coding.
The main effect of that proposed strange programming style is to confuse newcomers, as we've seen here already.

So -1 from me for the original feature request.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Protected 'already declared'

Post by Rinzwind »

The point being that dumping all protected variables at the top splits the code in two and looks messy. Would be handy if one could just declare a var near the place its used. Also makes reusing by good old copy pasting much easier. Sometimes a var is needed at two separate spots, so mentioning it twice would be handy syntactically and is already possible with global. So why not with protected...

Side note: Other languages allow you to declare vars scoped to a while-loop or if-branch.
Post Reply