Why this Lua function cannot be used with self :: "to be marked with" local "without getting" "(" expected next ":

Why this Lua function cannot be used with self :: "be labeled" local "without getting:

'(' expected near ':'

That is, in the code below everything works. But why can't I make the "scene: createScene" function local (since I get the above error when trying).

I note that listener functions must be performed locally. I sometimes encountered cross-scene problems in the storyboard. They can be marked locally and work fine.

SceneBase = {} function SceneBase:new() local scene = Storyboard.newScene() local function returnButtonTouch_Listener (event) -- code here end function scene:createScene( event ) -- WHY CAN'T THIS BE LOCAL??? -- code here end return scene end return SceneBase 

This is why the function string cannot be read:

  local function scene:createScene( event ) 
+4
source share
1 answer
 function scene:createScene(event) ... end 

It is syntactic sugar for:

 scene.createScene = function(self, event) ... end 

What is the syntactic sugar for:

 scene["createScene"] = function(self, event) ... end 

You want to do:

 local scene["createScene"] = function(self, event) ... end 

It does not make sense.


Another way: local is the qualifier for a variable that makes it local, not global. Which variable would you qualify with local function scene:createScene( event ) ?

createScene not a variable, it is the key in the scene table.


Actually, this is a bit misleading. When you speak:

 foo = 10 

Without qualification, foo becomes global, that is, it is stored in a global state as follows:

 _G.foo = 10; 

Which of course means the same:

 _G["foo"] = 10; 

When you use the local keyword, it is not stored in the table, it ends up being stored in the VM register, which is faster and has a more rigid limited area.

When you write one of them:

 function foo.bar() end function foo:bar() end 

You explicitly store the value of the function in a table ( foo ). These statements are exactly equivalent to these:

 foo["bar"] = function() end foo["bar"] = function(self) end 

I note that listener functions must be made local

What do you mean by that? In Lua, a function is a function. It is just another value, like a string or a number. Whether it is stored in a global, table, local, or not stored does not matter at all.

 local foo = print _G["zap"] = foo doh = zap t = { zip = doh } t.zip("Hello, World") -- Hello, World assert(print == foo and zap == foo and zap == doh and t.zip == doh) 

Here we pass the print function. This is still a function, and as long as we refer to it, we can call it.

I do not know Corona, but I assume that the event handlers are not defined by the local user naming convention. You need to register it as an event handler. For example, in accordance with this video, the Button object has an onEvent field that is set for the handler of this button.

+13
source

Source: https://habr.com/ru/post/1412473/


All Articles