[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: A Type Metatable for Tables?
- From: "John Hind" <john.hind@...>
- Date: Wed, 20 Feb 2008 12:14:53 -0000
Reading Roberto's "Programming in Lua" 2nd Edition, I was struck by the
inconsistency between the String library and the Table library - in Lua 5.1
the former's functions can be used as methods on a String object while the
latter can only be used as "traditional" library functions.
Of course the reason is that Strings have one per-type metatable while Table
metatables are per-instance. There is currently no way for the Table library
to set a metatable for all Tables.
This could be fixed by allowing Table to have *both* a type metatable *and*
an instance metatable. There would be two choices for how this could work:
1. Setting an instance metatable could displace the type metatable
completely. This would be appropriate if we think of Tables with instance
metatables as different types from generic Tables.
2. The instance metatable could be searched first, then the type metatable.
This would be appropriate if we think of Tables with instance metatables as
sub-types of Table.
Both alternatives could be offered by having a key in the instance metatable
specifying if the type metatable should be inherited or not.