Page 1 of 1

Should Define be allowed for procedure parameters?

Posted: Tue Jun 18, 2019 1:33 am
by Demivec
I had a hard time trying to name this topic. :)

Here is the reference code:

Code: Select all

Procedure bad(a = 5)
  Define a = 4 ;I'm don't think this declaration should be allowed
   
  ProcedureReturn a
EndProcedure

Debug bad(3) ;displays 4
I had never dreamed that you could redeclare a procedure's parameter. Yes it must be of a matching type but it can also have an initial value assigned. It would seem it is treated as a regular assignment and no error is thrown.

What are people's thoughts on this?

I am going to have to rewrite my rule book now to cover this so that it doesn't slip in by accident and cause a bug. :wink:

Re: Should Define be allowed for procedure parameters?

Posted: Tue Jun 18, 2019 2:03 am
by wombats
I don't think it should be allowed as it is a duplicate identifier. If you change Define to Protected or Static, it then throws an error.

Re: Should Define be allowed for procedure parameters?

Posted: Tue Jun 18, 2019 7:22 am
by #NULL
Redeclaration errors are not very consitent.

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

Re: Should Define be allowed for procedure parameters?

Posted: Tue Jun 18, 2019 7:42 am
by Mijikai
I always thought Define is only meant to be used for procedures (ex. Define.i Hello()).

Re: Should Define be allowed for procedure parameters?

Posted: Tue Jun 18, 2019 7:49 am
by #NULL
Mijikai wrote:I always thought Define is only meant to be used for procedures (ex. Define.i Hello()).
You're confusing it with Declare :)
Define is used with EnableExplicit for main-local variables (that are not arrays or lists), i.e. variables in the main scope that are not global / not visible in procedures (without Shared).

Define in procedures behaves like Protected.

Re: Should Define be allowed for procedure parameters?

Posted: Tue Jun 18, 2019 7:58 am
by #NULL
Also, you can't shadow a global with procedure-local Define, but you can with a parameter variable.

Code: Select all

Global g = 99

Procedure p1(g = 11)
  Debug g
EndProcedure

Procedure p2()
  Define g = 22 ; error: Variable already declared with a different scope
  Debug g
EndProcedure

p1()
p2()

Re: Should Define be allowed for procedure parameters?

Posted: Tue Jun 18, 2019 8:06 am
by Mijikai
#NULL wrote:
Mijikai wrote:I always thought Define is only meant to be used for procedures (ex. Define.i Hello()).
You're confusing it with Declare :)
...
My morning coffee wasnt strong enough :lol:
Thanks for waking me up :oops: