lua-users home
lua-l archive

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

On Mon, Jan 26, 2009 at 12:17 PM, Mark Tomczak <> wrote:
> Greetings all!
> We're considering a modification to the version of Lua embedded in our game
> engine that would allow for this construct (which is currently a syntax
> error in Lua):
> CallbackManager.postMyCallback(myInstance:instanceMethod)

For what its worth, I wasn't able to guess what this would do, knowing
vanilla lua, so I don't think its a very obvious extension.
Obviousness to a lua developer doesn't necessarily matter if your
users knew nothing of Lua before meeting your game, though.

> Under the present language rules, myInstance:instanceMethod is an error (as
> I understand it) because the colon notation is only valid in the context of
> function calls and function declarations, but is not valid in the context of
> specifying a function value. Currently, we can accomplish this goal by
> instead making the statement "CallbackManager.postMyCallback(function ()
> myInstance:instanceMethod() end)", but that seems to be a great deal more
> typing than should be necessary.

You could also do:

CallbackManager.postMyCallback(myInstance.instanceMethod, myInstance)

Which is only a bit more typing, and follows the common pattern of
assert() and pcall().

Or you could add a helper function:

CallbackManager.postMyCallback(bind(myInstance, "instanceMethod"))

function bind(instance, method)
return function () return (instance[method])(instance) end

And you can rename bind to something shorter, "B"?, if you are really
into saving characters.

Or, you can handle it at the other end:

CallbackManager.postMyMethod(myinstance, "instanceMethod")

Which is longer than your syntax extension by only two characters: