lua-users home
lua-l archive

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


----- Original Message -----
> > const t1 = {'a','b'}
>
> With current Lua syntax, it is possible to write a function
> "const" so that
>    t1={'a','b'}; const"t1"
> does what you want.

Would that make t1 immutable so it always refers to the same table? My Lua knowledge hasn't got that far yet.

> > t2 = const {'a','b'}
> …
> > t2[#t2+1]='x' would result in creating a NEW table with contains
> > all the old elements plus a new one. The old table remains
> > unchanged; it is immutable.
>
> Should not `t2[anything]` generate "Attempt to assign to an immutable
> table"?

That depends on how you decide to define it. You could define it to mean an attempt to change an immutable table which then generates an error, or you could define it to mean make a new copy of t2 with that change in it. I am used to the second. Either way is "right" just a long as you clearly define what happens. And are consistent.

Robert