lua-users home
lua-l archive

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


With all the talk in another thread about tables vs arrays the need for
unpacking was also mentioned. As many of you know I am working on a hard
fork of Lua which adds a new data type, the Index, which is essentially
an array of Lua types with a max size of 255. Support for directly using
an Index was also added to VM instructions, which was extended to 64-bit.

I am using this Index data type to implement a number of systems, including
structures, anonymous tuples, encapsulation of object and class variables,
variable function arguments, namespaces, and also providing access to fast
arrays of random Lua types (but limited in size).

With new data types we still need to unpack the structures, however, using
table.unpack won't work as an Index is not a Table. Nor would an Array or a
Hash be a Table should they be separated into distinct data types. To allow
unpacking of a variety of data types I was thinking of adding an "unpack"
operator, much as it seems Python has.

In Python the "*" operator unpacks only the values of the supplied variable,
where "**" unpacks both the keys and values of the variable. I was thinking
of adding such an operator to my Lua fork so that all supported variables
can be unpacked. I am trying to decide on the appropriate symbol to use and
was thinking "$" and "$$" as the "$" sigil is not currently used in Lua, and
the "$" looks like an "S", so "unpack to $tack" could be the mnemonic.

For example, I also plan to make "..." be directly usable via syntax instead
of creating a table first. Instead, my vargs will use the Index data type,
which itself will use a managed memory pool for speed.

This means that "#..." and "...[1]" and "$..." would work for length of the
vargs, getting varg #1, and unpacking all vargs. I suppose adding syntax for
array slices may also be useful for unpacking only some vargs, so "$...[2:4]"
could unpack only vargs 2, 3, and 4, for example. For unpacking a table the
"$" or "$$" operator could also take key names as in "$tbl{one,two,three}"
to only unpack the specified key names.

From a coding standpoint I can see the benefit of declaring a variable as
being only an array, or only a hash. From a compiler standpoint it could
also help for generating more optimised code... especially if, as the Ravi
author mentioned, the variable type for the entire array will be the same!

I just thought I would see what everyone thought of an "unpack" operator
since this discussion of tables vs arrays reminded me about my plans to
implement such an operator in the near future.

~Paige