lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Dec 18, 2009 at 12:07 PM, spir <denis.spir@free.fr> wrote:
> PS: There are certainly several techniques (eg thought at requiring a table for opt args). I'd like to review various possibilities.

roughly in order of personal preference:

- try to design the API so that it doesn't make sense to have more
than 1 optional parameter

- try to order optional parameters so that specifying one only makes
sense if the previous one is also specified.

- insert nil values: "func(val1,nil,nil,val4)"

- if there's few parameters, and the type makes it obvious which is
which, start the function by shifting them to make them match (this is
heavily used in some JavaScript libraries, jQuery in particular)

- if there are too many arguments, replace most of them with object state

- use a table as the only parameter, so the user writes "func{arg=val,
arg4=val4}" instead of "func(val1,val2,{arg5=val5})"

-- 
Javier