[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Enhanced tostring
- From: Edgar Toernig <froese@...>
- Date: Fri, 23 Oct 2009 04:39:14 +0200
Luiz Henrique de Figueiredo wrote:
>
> > Could someone remind me why Lua stringifies an object o by way of
> > getmetatable(o).__tostring(o) rather than o:__tostring()?
>
> In my libraries and probably many others, the two expressions are equivalent.
Yeah, but should they? At the moment "o" is a table and you add
arbitrary elements to it everything falls apart. Just look at the
recent thread "Tracking number of pairs in a dictionary" [1].
One suggestion was:
function make_LimitedTable(limit)
[...]
return setmetatable({}, <metatable with add, del, etc>)
end
> t = make_LimitedTable(2)
> t:add('a',{})
> t:add('b',{})
Guess what happens on
t:add('add',{})
And this was not the first time I saw this broken usage.
IMO it's time to drop the "colon is only syntactic sugar" dogma.
Ciao, ET.
[1] http://lua-users.org/lists/lua-l/2009-10/msg00007.html