lua-users home
lua-l archive

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

On Jul 7, 2015, at 4:25 PM, Daurnimator <> wrote:

On 8 July 2015 at 00:19, Jay Carlson <> wrote:
This conversation might have been shorter if somebody said:

"Tuples are a composite sequenced data type with value semantics."

Note that "value semantics" implies that the contents must also have value
semantics. This limits them to numbers, strings, booleans, tuples, and
possibly light userdata and nil.



Why can't you have a tuple that contains e.g. a string and a table?
It would be equal to any other tuple that has the same string and table inside.

Because the table content is mutable .. if you change the table you change the “value" of the tuple. Consider the OP request: memoization. The tuple repesents the value of a set of function arguments, as a key in a lookup table, while the table value for that key is the return result from the memoized function. If one of those arguments is a table, and the table value changes (which can happen at any time in any place in the Lua code), then memoization breaks (badly).

It also makes the entire table lookup problematic. What happens when the tuple changes to become equal to another, both of which are already in the memoization lookup table?

Tuples are not inherently immutable in the same way that (say) the number 10 is, but many languages have adopted this convention to provide the kind of determinism needed for tasks like memoization. You can think of them as an aggregate constant.