It is currently Wed Aug 21, 2019 3:34 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Should Define be allowed for procedure parameters?
PostPosted: Tue Jun 18, 2019 1:33 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3568
Location: Utah, USA
I had a hard time trying to name this topic. :)

Here is the reference code:
Code:
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:

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Should Define be allowed for procedure parameters?
PostPosted: Tue Jun 18, 2019 2:03 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 437
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Should Define be allowed for procedure parameters?
PostPosted: Tue Jun 18, 2019 7:22 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1019
Location: right here
Redeclaration errors are not very consitent.
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Should Define be allowed for procedure parameters?
PostPosted: Tue Jun 18, 2019 7:42 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 482
I always thought Define is only meant to be used for procedures (ex. Define.i Hello()).


Top
 Profile  
Reply with quote  
 Post subject: Re: Should Define be allowed for procedure parameters?
PostPosted: Tue Jun 18, 2019 7:49 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1019
Location: right here
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Should Define be allowed for procedure parameters?
PostPosted: Tue Jun 18, 2019 7:58 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1019
Location: right here
Also, you can't shadow a global with procedure-local Define, but you can with a parameter variable.
Code:
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()


Top
 Profile  
Reply with quote  
 Post subject: Re: Should Define be allowed for procedure parameters?
PostPosted: Tue Jun 18, 2019 8:06 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 482
#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:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: applePi, BlindMan, kenmo, Sirius-2337 and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye