lua-users home
lua-l archive

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


Edgar Toernig escribió:

> Wouldn't this be easier?

>   function printf(file, fmt, ...) write(file, format(fmt, ...)) end

> That is, allow the dots as arguments that expand to the passed varargs.
> The creation of the arg-table can be postponed until it is really
> needed (or even made by hand: local arg={...}).

I thought about that, actually. I was just searching around for a facility
already in the language which would help, and closures are already in the
language.

The syntax you suggest has some charm, though.

Perhaps in addition to local arg={...}, one might be able to say:

local argfn = function() return ... end

On the whole, though, I think it would be complicated to precisely define
the behaviour and cope with all the syntactic oddities. It is certainly the
case that a not-very-clever optimiser could deal with my "argfn" proposal
pretty well as easily. (A slightly more clever optimiser could probably
defer the creation of tables with the current syntax, but it's a lot
trickier because you are likely to end up with expressions involving
library functions (unpack, call) rather than syntactic primitives.

Anyway, I'd rather reserve the use of "..." outside of prototypes for
currying:

foo(a, b, ...) ==(def) function(...) return foo(a, b, argfn()) end

-- using the notation I proposed earlier.

Without further extensions, this only works for right-currying; a strict
definition of the above would allow left-currying (or intermediate
currying) only of single arguments:

foo(..., a, b) ==(def) function(x) return foo(x, a, b) end
foo(a, ..., b) ==(def) function(x) return foo(a, x, b) end

... more random thoughts, really.

(also, if functional composition and currying are both language primitives
rather than library functions, there are some nice reductions of composed
curries, etc.)

R.