Optional Parameters in Handlers

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Optional Parameters in Handlers

Post by n.allan » Tue Mar 17, 2015 10:18 am

How would I go about using optional handler parameters? In LCS the Vec() function below would accept 2 or 3 parameters depending on how you pass them. I have tried the following:

Code: Select all

public handler Vec(in tX as Real, in tY as Real, in tZ as optional Real) as Array

    variable tVec as Array
    put the empty array into tVec
    put tX into tVec["x"]
    put tY into tVec["y"]
    if tZ is not undefined then 
      put tZ into tVec["z"]
    end if
    return tVec
    
end handler -- Vec

private handler testVec() as Array

    variable tVec as Array
    put the empty array into tVec
    -- put Vec(1,2) into tVec -- This throws compiler error "too few arguments"
    put Vec(1,2,3) into tVec -- works OK
    return tVec

end handler -- testVec
If I only pass 2 arguments to Vec() the compiler complains even though z is declared as "optional".

Any ideas on the best approach? I would like to access the Vec() function from LCS and LCB to see if I can get some speed improvements.

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1212
Joined: Thu Apr 11, 2013 11:27 am

Re: Optional Parameters in Handlers

Post by LCMark » Tue Mar 17, 2015 3:33 pm

@n.allan: At the moment 'optional' only affects types it doesn't affect syntax - so if you have 3 parameters declared for a handler, you need to provide it with 3 parameters. What you are asking is (I think) better thought of as 'default parameter values' as it is the more general concept. I've filed an enhancement in the RQCC for further consideration (http://quality.runrev.com/show_bug.cgi?id=14987).

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Re: Optional Parameters in Handlers

Post by n.allan » Tue Mar 17, 2015 4:35 pm

Yes exactly. Default parameters would be useful. Thanks.

Post Reply

Return to “LiveCode Builder”