lua-users home
lua-l archive

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


It seems like the big question would be when binding happens.

t = {a = function(self) return 1 end}
f = t:a
t.a = function(self) return 2 end
print(f()) -- 1 or 2?

Not a reason to avoid; just something to think hard about. (1) sounds like the more Lua-ish approach, but it limits your ability to change event handler behavior after registration without yucky extra syntax. (2) gives you that ability, but makes f=t:a a very different sort of statement than f=somefunc.

Ben

On Mon, Jan 26, 2009 at 3:17 PM, Mark Tomczak <mtomczak@wildpockets.com> 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)

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.

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'm nervous about getting halfway down the road of trying out this modification and then running headlong into a language ambiguity.

Thank you for the input!

-Mark