[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Multidimensional Arrays
- From: Lorenzo Donati <lorenzodonatibz@...>
- Date: Fri, 22 Jul 2011 21:26:27 +0200
On 22/07/2011 19.11, Steve Litt wrote:
[...]
If I understand the situation correctly, Lua has exactly two types for
complex data:
1) Tables
2) Metatables
All other complex data is built from those two and doesn't exist in
the language.
Nope. The only "structured" data type are tables. Metatables are not
another type. Metatables are plain Lua tables. They are attached to
other types in order to modify the behaviour of some operations on those
types.
Every table can have its own metatable (which, as I said, is a plain
table itself). Example:
meta = {} -- plain table
t = {}
-- the following sets the table "meta" as the metatable for table "t"
setmetatable( t, meta )
The previous example is fairly silly, because an empty metatable doesn't
do much.
A more interesting case is this:
meta = {}
meta.__call = function( self, ... )
-- the first arg (self) gets the
-- table which was "called" (see below)
print( ... )
end
t = {}
setmetatable( t, meta )
In this case the metatable has a so-called metamethod "__call" which
renders the table on which the metatable is attached "callable". When
"called" the table t dispatches its arguments to the metamethod
"__call", so that the call:
t( "hello", "there", 3.14 )
is equivalent to the call:
meta.__call( t, "hello", "there", 3.14 )
and thus prints:
hello there 3.14
Adding appropriate metamethods to "meta" above you can customize many
other aspects of the behaviour of "t", but "meta" remains still a plain
table.
Note: only tables and userdata can have individual metatables, all other
types have a single metatable per type (not per-object).
Anyway, to set a metatable for types other than tables, you must use
debug.setmetatable or resort to C API.
[...]
SteveT
-- Lorenzo