lua-users home
lua-l archive

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


>Sunshilong, If you want something like named parameters (aka named
>arguments or keyword arguments), you can emulate that in Lua using
>tables:
>   foo{a = 10} -- error: parameter 'b' is required
>   foo{a = 10, b = "meh"} -- error: parameter 'b' must be a number
>    foo{a = 10, b = 123} -- error: parameter 'b' must be greater than
> 0 and less than 100
>    foo{a = 10, b = 20} -- ok
>That table is passed as the first argument into 'foo'. Not exactly
>what you wanted, but I think you wanted something like this.

It's a solution. But, maybe, not a good one.

You see, the parameters passed to the C function are tables already,
e.g:

carState =  {};
personState = {};
roadState ={};

calc(carState, personState, roadState)

It's not a good idea to replace the aforementioned code snippet with
another one like this:
calc({`carState`=carState, 'personState'=personState, `roadSate`=roadState).

The aforementioned tables(i.e carState and etc) are  already complicated.
It's not a good idea to put these tables into another table.

One more question, is there a clerical error in your last email?
I think it should be foo({a = 10, b= 129}) instead of foo{a = 10, b = "meh"}.

Best regards
Sunshilong