lua-users home
lua-l archive

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

> On Dec 22, 2009, at 11:25 AM, Javier Guerra wrote:
> > On Tue, Dec 22, 2009 at 2:16 PM, spir <> wrote:
> >> Again: why aren't metamethods identified by name?
> > 
> > you keep repeating this; but i really don't get what you mean.  either
> > you defined your request somewhere else and i missed it, or you think
> > it's too obvious to state.  but i _really_ don't have the faintest
> > idea what this could mean.
> I think what Denis is asking is why we can't just do things like put
> __tostring in the object/table itself and instead we have to put it in
> the metatable.

You can. Just set the table as its own metatable.

> That would certainly make one-off object creation easier. I don't know
> what the performance impact would be in general and I don't know what
> the other considerations were that led to the current design.

I think one-off objects are the exception, not the norm. For what seems
to be the normal case (many objects sharing one or more metamethods), a
separated metatable may be a great saving.

In the beginning, "index" and "newindex" (then with different names)
could not be tables, only functions, so sharing metamethods was even
more common. People did not mind paying the price of a function call for
a table access: that was the standard price.

Then Lua introduced the possibility of tables in "index" and "newindex".
Suddently, the price of a function call became prohibitive. Of course it
is exactly the same price, but comparatively it was too expensive. Now
most programmers avoid using functions for "index" and "newindex"
metamethods. A change that was supposed to speedup a special case ended
up restricting the flexibility of the language.

-- Roberto