lua-users home
lua-l archive

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


On 09/07/2011 18.20, HyperHacker wrote:
On Sat, Jul 9, 2011 at 10:14, Dirk Laurie<dpl@sun.ac.za>  wrote:
On Sat, Jul 09, 2011 at 05:38:59PM +0200, HyperHacker wrote:
On Sat, Jul 9, 2011 at 09:33, Dirk Laurie<dpl@sun.ac.za>  wrote:
On Sat, Jul 09, 2011 at 04:58:10PM +0200, HyperHacker wrote:
That one can easily do it with a metatable especially
makes me wonder why it isn't done by the library itself
If this argument were valid for coroutines, it would apply equally
to tables:  why must one say table.insert(list,item) instead of just
list:insert(item)?

To answer your question: it is not done by the library itself
_because_ one can easily do it with a metatable.

"Less is more" — Mies van der Rohe (1886–1969), architect.

Dirk



I'm pretty sure it's not done with tables because list:insert(item)
already has a different meaning (to look up 'insert' in the table
'list'), and to set up a metatable like that could be confusing. (Why
t:insert() doesn't work? Oh, because t has a key named 'insert'
already... now is t2.insert a key in t2 or in table? etc)

Sorry, I did not absorb your whole post when writing the reply
above.  Tables are indeed different in an important way.

The interesting question to me is, since it is so easy to say
debug.setmetatable('',{__index=string}), why the equivalent of that
has been built in.  There must have been a debate on that a few
years ago: could an old-timer tell us the clinching reason?

Dirk




That's a big part of my confusion too: why this is built in for one
type but not another...

Just my 2 eurocent:

Lua tend to be simple, so no metatable trickery by default. Especially for tables, since _each_ table can have its own metatable.

Moreover tables are the only type for which metatable trickery is favoured on Lua side (for other types you have to resort to debug.metatable) and providing a default, common, metatable for every table would be a big hassle for client code.

I think that string is an exception only because string functions are *so* commonly used that that was a sort of compromise for the sake of convenience.

BTW, I've seen lot of Lua code where the OOP style for strings is not used, i.e.: string.match(s, pattern) instead of s:match(pattern), so it's something pertaining to own's programming style.

Cheers.
-- Lorenzo