#Null a procedure parameter?

Just starting out? Need help? Post your questions and find answers here.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

#Null a procedure parameter?

Post by wayne-c »

I have the following procedure:

Code: Select all

Procedure Test(Text$, Map Args$())
...
EndProcedure
Sometimes I do not have the Map available, thus I would like to call the function as follows:

Code: Select all

Test("Hello World", #Null)
This is not possible and results in a compile error: "Bad parameter type: a map is expected."

Is there any workaround or best practice to handle such a case?

This is possible however it seems like a bad hack to me:

Code: Select all

Global NewMap DummyMap$()

Procedure Test(Text$, Map Args$())
...
EndProcedure

Test("Hello World", DummyMap$())
When working with the dummy map, I cannot find out inside the procedure if there was a real map or the dummy map passed (to be able to skip the functionality whenever the dummy map is passed)

Any ideas folks?
As you walk on by, Will you call my name? Or will you walk away?
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: #Null a procedure parameter?

Post by RSBasic »

Maybe with this code: viewtopic.php?p=531868#p531868
Image
Image
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: #Null a procedure parameter?

Post by wayne-c »

RSBasic wrote:Maybe with this code: viewtopic.php?p=531868#p531868
That would mean to wrap all parameters inside a wrapper structure, right? Well I hope there is an easier approach out there...
As you walk on by, Will you call my name? Or will you walk away?
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: #Null a procedure parameter?

Post by skywalk »

If your Procedure expects a Map(), then check for the Map() before calling the Procedure.
Or create a structure that holds the Map() and set a structure parameter like \useMap = 0|1.
Then pass the structure to the Procedure.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4089
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: #Null a procedure parameter?

Post by Demivec »

You can simply test for an empty map ,

Code: Select all

If MapSize(Args$()) = 0
     ;no arguements
EndIf
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: #Null a procedure parameter?

Post by wayne-c »

Demivec wrote:You can simply test for an empty map ,

Code: Select all

If MapSize(Args$()) = 0
     ;no arguements
EndIf
Unfortunately not; my function receives an empty map and fills it inside. But if the caller does not want the map to be filled, it just uses power/memory for nothing.

But passing a dummy map and an additional parameter useMap=1|0 will do the trick; however to declare and passing an unused map is imho a bit hacky. #Null - as e.g. in Objective-C - would just make more sense here.
As you walk on by, Will you call my name? Or will you walk away?
Post Reply