Should Define be allowed for procedure parameters?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Should Define be allowed for procedure parameters?

Post 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:
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Should Define be allowed for procedure parameters?

Post 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.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Should Define be allowed for procedure parameters?

Post 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
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Should Define be allowed for procedure parameters?

Post by Mijikai »

I always thought Define is only meant to be used for procedures (ex. Define.i Hello()).
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Should Define be allowed for procedure parameters?

Post 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.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Should Define be allowed for procedure parameters?

Post 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()
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Should Define be allowed for procedure parameters?

Post 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:
Post Reply