lua-users home
lua-l archive

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


On Mon, Sep 27, 2010 at 3:59 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> But library code should always check for  'bad self', at least in an
> ideal universe ;)

Even if 't' and the other table have the same type, it's still not a
good thing. Lets take inheritance as an example: Assume I have a type
Foo and a derived type Bar, each with a method 'baz', and instances
'f' and 'b' respectively. I expect b:baz() to call Bar's baz on b. But
if I used f.baz(b) instead, I'm actually calling Foo's baz on b. And I
can't directly disallow this because if Bar didn't define its own
'baz', it would be perfectly valid for b:baz() to call Foo's baz
instead.

> Closures can be expensive for _lots_ of objects of the same class.

I define the methods once as locals, and use smaller closures to call
the method with the object as an argument. It can potentially create
lots of closures, yes, but the waste is kept to a minimum. And I like
the assurance that it can't be circumvented without access to the
debug table (because I use newproxy() to create the objects, and I put
data in the userdatum's environment).

~Jonathan