lua-users home
lua-l archive

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


On Sat, Aug 13, 2011 at 12:52:33AM +0200, Lars Doelle wrote:
> 
> reviewing the tables, i found them limited w.r.t. composed keys.
> To ensure, all keys stay the same during their existience, tables
> as possible (and only) structuring technic are only identified
> by their pointer (i.e. object name).
> 
> --
> 
> Practical example:
> 
>   Key1 = { first="John", last="Doe" }
>   Key2 = { first="John", last="Doe" }
> 
> When using a key like the above for an table, currently Key1
> and Key2 would identify different entries, i.e. are of no use.
> 

You want something like Python's 'tuple'.

<python>
    s = {}
    s[(1,1)] = 11
    s[(2,3)] = 23
    i=2; j=3; s[(i,j)]=s[(1,1)]
    print s[(2,3)]   # --> 11
</python>

> My proposal to resolve the issue adding a 'constant' attribute
> to tables. Technically, this would work as follows:
> 
>   Table.todata( tbl ) : produces a 'constant' copy of 'tbl'
> 
> The effect is basically the same as it now for creation of strings.
> All constant tables are kept in a global map guaranteeing their
> uniqueness.
> 

No, the result of Table.todata will be not be a table with an extra
attribute, it will be a new table-like userdata having only a subset 
of the properties of tables.

Now here's the rub:  Lua doesn't have Python's syntax for tuple values.  
The above code becomes:

<lua>
    s = {}
    s[Table.todata{1,1}] = 11
    s[Table.todata{2,3}] = 23
    i=2; j=3; s[Table.todata{i,j}]=s[Table.todata{1,1}]
    print s[Table.todata{2,3}]   --> 11
</lua>

Even a preliminary `local T=Table.todata` is not going to make that
look beautiful.

So IMHO nothing short of fully implementing immutable tables as another 
Lua data type will meet your request.  Keep plugging away, immutable
arrays (i.e. tuples) are already down below in Lua, and the tip of the 
iceberg is showing on the surface as `...` and `select`.  Probably not 
5.3, if indeed there will ever be a 5.3, but who knows, Lua 6.0 could 
have it.  Only ten more years to wait …

Dirk