lua-users home
lua-l archive

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


Rici Lake wrote:

>> <devils advocate>except for ..., which appears to be a non-tabular
>> compound data structure</devils advocate>
>
>Indeed, but it is not in fact a first-class value. On the other hand, the
>fact that it appears to be useful might indicate that tuples are a
>worthwhile idea.
>
>In fact, Lua has several compound datatypes other than tables, including
>threads and functions, and as has been demonstrated, tuples can be
>modelled with functions, so a new datatype is not necessary to provide 
>the facility.


The problem with this is that functions have object-like identity.

a = function() end
b = function() end
print("a==b :" .. (a==b))	-- a==b : nil

Tuples are desirable as higher-order table indices, but need to be indexed 
by value, so they should be immutable and interned like strings!

Interning should also benefit performance due to fewer intermediary table 
constructions.

Garbage collection gets more hairy if you have tuples referencing tables, 
but "weak" or "mutable" tuples are a mess that solve nothing.  Weak tables 
already do this job.

You can generate "magic" strings as higher-order indices, and hold extra 
references to prevent GC, but this is very ugly.

Tuples could replace the magic expression lists (return values and ...), 
and be implicitly unpacked in useful cases (unless .

I don't have any great syntax ideas.  <> are already used in operators, 
and [] in table indexing.  Maybe \{}.

Implementation-wise, regular tables with added restrictions could do most 
of the work, and then you borrow interning hash-value from strings (made 
recursive for nested tuples).  Or to look at it another way, strings could 
be a special case of tuple.

Lastly, tuples could also solve help the issues of using tables as arrays 
(with holes), since they have fixed length (like strings).  This helps for 
loops too, since loop variables and table indices are already effectively 
unassignable during traversal, since assignment results in undefined 
behavior. Then "for k,v in u do ... end" could be reinstated for tuples.

-Lucas