lua-users home
lua-l archive

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

> Where this thread seems to be headed is toward defining a standard userdata
> type "array".
>     a = array( 2, nil, "foo" )
>     #a -- yields 3
>     a:insert( "baz" )
>     a[ 4 ] -- yields "baz"
>     a:unpack() -- yields 2, nil, "foo", "baz"
>     for i, v in a:ipairs() do
>         print( i, v )
>     end
> [...]
> Of course, now we've moved from one table to three in order to represent an
> array and we've lost the simple uniformity of having only one data type. So,
> I'm not sure it's an improvement.

Another option is to implement tuples. We can implement them as C
functions with upvalues (PiL2, p. 257). With a few extra lines of code,
we get the following:

  a = new.tuple(2, nil, "foo")
  #a   -- not available; perhaps a"#" ???
  a:insert("baz")   -- not available (tuples are immutable)
  a(3)    -- yields "foo"
  a()     -- yields 2, nil, "foo"
  for i, v in a do ...    -- or something like this...

The performance is quite good and we do not create a new data type. They
are immutable, which restricts their uses (this may be a good
thing). But they seem to cover the basic problem here, which seems to be
the capture of multiple argments and multiple returns (or, as Diego put
it, '{...}' and '{f()}').

-- Roberto