lua-users home
lua-l archive

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


As discussed recently, another way of getting around this idiom is to add a
standard ieach function which returns index, value for each argument it
receives including nil values.

    for i, v in ieach( ... ) do

    end

Upside:
    Clearer
    Fewer copy steps for the values in ...

Downside:
    Allocates a closure and possibly a table

Mark

on 9/14/05 1:29 PM, Mark Hamburg at mhamburg@adobe.com wrote:

> Yes, but one could get the length of the first item by writing #(...).
> 
> It seems like #... would be fairly useful assuming the implementation has
> ready access to the length of ...
> 
> That being said, I also understand that this would further complicate the
> grammar since ... wouldn't always be an expression. It would essentially
> require the introduction of a non-terminal for
> expression-other-than-dotdotdot.
> 
> Mark
> 
> on 9/14/05 1:09 PM, Roberto Ierusalimschy at roberto@inf.puc-rio.br wrote:
> 
>>> Perhaps I missed this?  What is its meaning?
>> 
>> ... is an expression. #exp returns the length of the result of exp.
>> So, #... is the length of the first argument of a vararg function.
>> Try this:
>> 
>> function f (...) return #... end
>> print(f("hi", 4, 5))
>> 
>> 
>> Similarly, <<for i, v in ...>> also has a meaning. See next example:
>> 
>> function f (...) for i,v in ... do print(i,v) end end
>> f(pairs{2,4,5})
>> 
>> -- Roberto
>