[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Syntax sugar for default arguments
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sun, 21 Apr 2013 19:45:46 +0200
2013/4/21 Steve Litt <slitt@troubleshooters.com>:
> By the way, are you going to let the other shoe drop and do the same
> thing in the subroutine *call*?
>
> function f(x=3, y=4)
> whatever()
> end
>
> f(y=7) -- no need to articulate x
This is immeasurably harder. The other one is mere syntax sugar, a trivial
modification to the source string before passing it along.
This requires code to examine run-time debugging information. At compile
time it is not known which function object is currently associated with the
name f. Therefore it is not known which parameter of f was called y when
the function was defined. One will have to generate code that repeatedly
calls debug.getlocal and works through all the local variables one by one.
All this, for a simple function call.
What will be easier is default values for entries in keyword-style
table arguments:
function f{x=3,y=4}
whatever()
end
This will translate to the equivalent of:
function f(param)
for k,v in pairs(default[f]) do f[k]=f[k] or v end
whatever()
end
default[f]={x=3,y=4}
default_parameters[f]