lua-users home
lua-l archive

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


>
> Immutable tables and immutable variables would be a valuable
> addition.

Let's think about implementation.

Immutable tables, easy.  Put something in the metatable to mark it.

Immutable variables, not so easy.  Variables are just entries in _ENV,
and their names are just the keys.  _ENV can be any table.  You need
to keep the information "in this table, assignment to or deletion of
this key is not allowed".  `Node` has no spare bits.

So, we may as well implement immutable tables and variables at
the Lua level.  A field `__immutable` in the metatable will serve
nicely.  If it is a table, it contains a list of immutable names.
If it tests `false` nothing about it is immutable.  If it is `true`,
the whole table is immutable.

Of course, any table exploiting `__immutable` will have to be
a proxy table so you can catch __newindex.

Everything can go into a module.  The user will see:

----
immutable = require "immutable"

reserved_names = immutable(_ENV,{})
reserved_names.x = true  -- x is now immutable
----

I'll sit down and write the code next time I feel I really need these
immutable thingies.   Which probably means never.