lua-users home
lua-l archive

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

On 8 July 2015 at 10:34, Tim Hill <> wrote:
> 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).

I don't think that's a good assumption.
In the case of memoisation: just as often I want to memoise based on
if I was given the same table, as if I was given the same table
If a programmer needs to memoise based on table contents, it should be
up to them to serialise it.

> 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?

How could a tuple change? they're immutable...

> 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.


I'm saying a tuple should be an immutable collection of it's arguments.
This means that a given tuple always has the same contents.
However, the contents can change.
These tests should pass:

local s, t = {}, {}
assert(tuple("foo", s) == tuple("foo", s))
assert(tuple("foo", s) ~= tuple("bar", s))
assert(tuple("foo", s) ~= tuple("foo", t))
local tmp = tuple("foo", s)
s[1] = "testing"
assert(tuple("foo", s) == tmp)