[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [Qs (a bit bent, I admit)] regarding the behaviour of non standar d contents of a metatable
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 20 Sep 2002 11:55:18 -0300
> I was wondering: what misbehaviours I should expect if I set non
> event-related fields of a metatable ?
The metatable is a regular table, and can be used for any mean.
> Is this endorsed by the language and will work as it should be expected
> from any table, or is there some problem regarding a special handling of
> metatables I am not aware of ?
Such use is completely "legal". The only small implementation detail is
that Lua assumes that metatables do not change very frequently. If they
do, some metatable tests (basically when accessing a table with that
metatable but without an __index field) may slow down a bit.
> setmetatable(bob,t1)
> [...]
> Since bob is a table and field b does not exist, __gettable get its
> metatable t1. Since t1 is non-nil and not a function, __gettable tries to
> get t1[b], which should yield 2.
Not so fast. "bob.b" will try to index the __index field in the metatable,
not the metatable itself! So, your example should read
setmetatable(bob,{__index=t1})
-- Roberto