Optional Parameters in LCB

Moderators: LCMark, LCfraser

Post Reply
PaulDaMacMan
Posts: 128
Joined: Wed Apr 24, 2013 4:53 pm

Optional Parameters in LCB

Post by PaulDaMacMan » Sat Sep 03, 2016 2:17 am

I'm working on an LCB Library and I want to have a function that returns something even if no parameter is passed (or something else is the correct switch keyword is passed)

Code: Select all

handler getMyThing(pSwitchForOtherThing) -- no type so it should be optional any even nothing 
     if pSwitchForOtherThing = "other thing" then
         return "bar"
    else
         return "foo"
    end if
end handler
so that if I call getMyThing() I get "foo" back
but if I call call getMyThing("other thing") I get "bar" back

but this doesn't compile, errors that there aren't enough parameters.

Also if I want to have a param and an optional second parameter, in LCS I still have to include a comma after the non-optional first parameter in my call, even if the second param isn't needed like so : getMyThing("other thing", empty )

Is this not possible yet or are these bugs, or just haven't been refined yet, or what?

Also can we please have CASE/SWITCH in LCB, Pretty Pleeease?

Thanks

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1553
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Optional Parameters in LCB

Post by [-hh] » Thu Nov 03, 2016 12:37 am

Just some thoughts.
PaulDaMacMan wrote: ... if I want to have a param and an optional second parameter, in LCS I still have to include a comma after the non-optional first parameter in my call, even if the second param isn't needed like so : getMyThing("other thing", empty )
Simply use ONE list as param. This list may have 0,1,2,...,N elements.

Code: Select all

private handler anyThing(in pParam as List) returns optional any
    if mSwitchForOtherThing is "other thing" then
        return "bar"
    else if pParam is the empty list then
        return "foo"
    else
        return pParam[1] -- is 42
    end if
end handler
shiftLock happens

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 555
Joined: Fri Jun 27, 2008 9:00 pm

Re: Optional Parameters in LCB

Post by Mikey » Thu Nov 03, 2016 2:15 pm

Oh that's sexy.

PaulDaMacMan
Posts: 128
Joined: Wed Apr 24, 2013 4:53 pm

Re: Optional Parameters in LCB

Post by PaulDaMacMan » Wed Nov 23, 2016 12:38 am

That's an option, then I just have to coerce the elements of the list into the required types (integer, string or empty).
Thanks for the suggestion.

Post Reply

Return to “LiveCode Builder”