Page 1 of 2

Protected 'already declared'

Posted: Mon Dec 09, 2019 5:48 am
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.

Re: Protected 'already declared'

Posted: Mon Dec 09, 2019 6:38 am
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.

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 2:09 pm
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()
:?:

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 2:34 pm
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.

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 2:35 pm
by mk-soft
With Protected the variable is defined at compiler time the memory space on the stack, not at runtime by the program

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 2:42 pm
by nsstudios
Thanks @Little John   and @mk-soft, my bad.
:oops:

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 2:56 pm
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)

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 3:38 pm
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.

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 4:00 pm
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.

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 4:10 pm
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).

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 4:44 pm
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.

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 5:47 pm
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.

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 6:05 pm
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.

Re: Protected 'already declared'

Posted: Sat Jan 25, 2020 7:14 pm
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.

Re: Protected 'already declared'

Posted: Sun Jan 26, 2020 3:34 am
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.