lua-users home
lua-l archive

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


Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:

> On Thu, Nov 30, 2017 at 2:37 PM, Dirk Laurie wrote:
>> Does a patch that allows the clumsy idiom
>> 
>>     function(a,b,c)
>>       a = a or 0
>>       c = c or 1
>> 
>> to be replaced by
>> 
>>     function(a=0,b,c=1)
>> 
>> already exist?
>> 
> At what moment of time those "default expressions" would be evaluated?
> At the moment of definition or at the moment of function invocation?
> 
> function pretty_print_time(unix_time=os.time())
> 
> Are they allowed to refer to each other?
> 
> function string.byte(str, from_pos=1, to_pos=from_pos)

I would have to say at invocation if the values are allowed to be
expressions rather than constant values.

So, essentially function(a=expr1, b, c=expr1) would just become
syntactic sugar for the following:

function(a, b, c)
  a = a or expr1
  c = c or expr2
end

This might be difficult to do with the standard Lua parser as there is
no real way to defer the expressions declared in the function parameter
declaration. However, with my patch that adds the ability to store tokens
for playback later this would be a trivial patch to implement. Including
the ability for later parameters to use earlier parameters as defaults.

~Paige