lua-users home
lua-l archive

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


On Fri, Nov 16, 2012 at 12:39 PM, spir <denis.spir@gmail.com> wrote:
> It came to my mind that it is easy in Lua to have kinds of structs, objects
> with predefined field structure that they thus do not need to hold
> themselves, instead they just hold data. like C structs, Pascal records,
> Python namedtuples (actually unnamed). Is there any common or known
> implementation of such things in Lua?

I also thought it was useful to have structs:

http://lua-users.org/wiki/StrictStructs

altho the constructor still uses the field names (easy enough to make
constructor to accept values but then the struct def needs to be
ordered)

The main benefits are:
  - a typo in a fieldname _read_ access is an error, not a silent nil
  - each struct type is unique and can be used in documentation

For a little loss of speed (as you say) we can use the proxy pattern
and then also make it an error to write to an unknown field

steve d.