4

I am learning Lua and came across the following construct:

button.action = function() buttonPressed() end 

Is it the same as

button.action = buttonPressed() end 

?

I understand that button.action is assigned a value returned by buttonPressed(), but why wrap it into anonymous function like that?

2
  • 4
    Your second snippet looks incorrect or incomplete. There's an end for closing a block scope but nothing starting it. Are you sure that's the full code? Commented Sep 2, 2013 at 8:59
  • You are right, thank you. Now I understand how and why it is wrong. I made a change from a working snipped without realising that 'end' must be removed in this case. Commented Sep 3, 2013 at 2:44

3 Answers 3

3

This:

button.action = function() buttonPressed() end 

is (almost) the same thing as this:

button.action = buttonPressed 

But note the absence of parentheses at the end.

As @hjpotter92 said, the main difference is that buttonPressed could return something whereas button.action does not.

Sign up to request clarification or add additional context in comments.

1 Comment

One more big difference exists. The former will depend on future changes of buttonPressed's value, the latter will not depend.
2

"I understand that button.action is assigned a value returned by buttonPressed(), but why wrap it into anonymous function like that?"

You got it wrong. The statement:

button.action = function() buttonPressed() end 

assigns to button.action an anonymous function that, when called, will in turn perform the call buttonPressed(). Note that the anonymous function doesn't return anything, so it is called only for its side effects. This is a common idiom with callbacks. You use an anonymous function to delay the execution of some piece of code (in this case, only the call to buttonPressed) until you need to execute it.

Given the names of your snippet I guess that this code sets the action to be performed when some button is pressed. When the button is pressed, then the action is fired (somewhere in the bowel of the code there will be a call like button.action() that calls the anonymous function stored in button.action) and the call buttonPressed() is performed.

1 Comment

Thank you, that made sense (I think). I still need to think it through and experiment to get a better grip on it.
1

Lua on its own has no concept of private/protected variables. Various programmers use various methods to emulate the same result.

As for your interpretation of the construct; there is a mismatched end statement. Even ignoring that syntactical error, the value of button.action will not be that returned by buttonPressed function. Here's a small demonstration to show you:

x = function() return 'hi' end y = function() x() end z = x() print( y, z, y(), 1 ) 

As you can see, it's most probably the programmer's way to incorporate a "protected" function or class (in my opinion).

1 Comment

Thank you for the demo, it helped clearing things a lot. I believe in this case the aim is to have a universal button class and be able to assign what action the button performs when creating an instance of a button. Thus this construct is used outside of the class code to store desired action in one of class properties, instead of defining inside the class what action would be performed and thus loosing flexibility.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.