lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark Tomczak wrote:
[...]
> I picked the IRC channel's collective brain on this issue, and we
> discussed the performance impact aspects of this design. But I also
> wanted to ask the mailing-list community: is there
> any syntactical reason that I would want to avoid allowing
> myInstance:instanceMethod to become a valid construct outside of a
> function declaration (as, for instance, a shorthand alias for
> constructing the closure as I described)?

I can't spot any syntax issues offhand. However, you may not be aware
that Lua actually supports several different OO models, and one of these
uses closures for all methods. This means you can call them with .
rather than :, and every object contains a unique set of closures
specific to that object, so you don't need to pass around a self object
any more. This allows:

CallbackManager.postMyCallback(myInstance.instanceMethod)

Advantages to this approach include it being faster, as most object
state can now be accessed via upvalues rather than table lookups, and
it's got more familiar syntax. Disadvantages are that it uses more
memory, as if you have 1000 instances of an object with 10 methods
you'll need 10000 unique closures.

Oh, it would also involve restructuring most of your code to use this,
which you may not be keen on!

- --
David Given
dg@cowlark.com

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkl+InUACgkQf9E0noFvlzgmuwCg1v+Enk0/W6/+2KQdzZ+AMEa0
0KoAoML/BU58lSeYMHEECMMZFQnkiBHE
=Ue41
-----END PGP SIGNATURE-----