Page 1 of 1

Optional Parameters in Handlers

Posted: Tue Mar 17, 2015 10:18 am
by n.allan
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.

Re: Optional Parameters in Handlers

Posted: Tue Mar 17, 2015 3:33 pm
by LCMark
@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).

Re: Optional Parameters in Handlers

Posted: Tue Mar 17, 2015 4:35 pm
by n.allan
Yes exactly. Default parameters would be useful. Thanks.