lua-users home
lua-l archive

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


> Proposal 2
> ----------
> Leave tables as they are, but introduce more efficient mechanisms than
> select for dealing with varargs. Specifically, allow:

<snip>

> Proposal 3
> ----------
> Tuples to capture sequences containing nil. This can be done with functions
> but they are crude because they don't support #tuple and tuple[ idx ]. This
> can be done with userdata but it gets a bit expensive and leaves open the
> question of why one needs to bail out to userdata to work around language
> "issues".

I think your proposal 3 would be an implementation of proposal 2 in the sense
that (functional) tuples could substitute select. Why would you need #tuple
and tuple[idx]? select does not have them, for instance. With tuples one could
do:

> t = tuple.new(10, 20, nil, {}, "foo")
> print(t"#")
5
> print(tuple.len(t))
5
> print(t())
10      20      nil     table: 0x8082d30        foo
> print(t(3))
nil     table: 0x8082d30        foo
> print(t(2,4))
20      nil     table: 0x8082d30

However, if #tuple and tuple[idx] are really needed, one could set a metatable
for functions:

> debug.setmetatable(t, {
>> __len = function(o) return o(0) end,
>> __index = function(o, k) return o(k, k) end})
> print(t[#t])
foo

Cheers,
Luis.

-- 
A mathematician is a device for turning coffee into theorems.
        -- P. Erdos 

-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>