Lua: How to call a function before defining it?

What is the syntax for creating a function, but then add it further to the code?

So something like this:

  • Define doX function
  • doX call (hereinafter in code)
  • doX implementation (i.e. all functions down at the bottom of the file)
+4
source share
3 answers

oh ... so there really is no way to call funcName before it really defines the function? those. do you still need to make sure that defineIt is called before the first call to the funcName function?

I wanted to clarify this point, and I felt that the answer would be a better way than commenting.

Lua is a much simpler language than C or C ++. It is built on some simple foundations, with some syntactic sugar, to make parts easier to digest.

Lua has no concept of a function definition. Functions are first-class facilities. They are values โ€‹โ€‹in Lua, just like the number 28 or the string literal "foo" are values. A โ€œfunction definitionโ€ simply sets a value (namely, a function) to a variable. Variables can contain any value, including the value of a function.

All "function calls" take a value from a variable and try to call it. If this value is a function, the function is called with the specified parameters. If this value is not a function (or a / userdata table with the __call __call ), then you get a runtime error.

You can no longer call a function that was not set in a variable, but you can do this:

 local number = nil local addition = number + 5 number = 20 

And we expect that the addition will contain 25. This will not happen. And therefore, for the same reason, you cannot do this:

 local func = nil func(50) func = function() ... end 

As Paul pointed out, you can call a function from another function that you define. But you cannot execute the function that calls it until you populate this variable with what it should contain.

+7
source

You only need to have a variable for reference. local funcName enough for your purposes with one clause. This will work:

 local funcName function callIt() print(funcName()) end function defineIt() funcName = function() return "My Function" end end defineIt() callIt() 

As long as you define it ( defineIt ) before calling it ( callIt ), it should work as expected. You cannot do something like this (and this is a warning):

 local funcName print(funcName()) funcName = function() return "My Function" end 

You will receive an error message: attempt to call local 'funcName' (a nil value) .

+10
source

As others wrote, you cannot call a function at run time that was not assigned before the call. You must understand that:

 function myFunc() print('Something') end 

It is only syntactic sugar for this:

 myFunc = function() print('Something') end 

Now it makes sense that such code will not work the way you want:

 print(greeter(io.read())) -- attempt to call global 'greeter' (a nil value) function greeter(name) return 'Hello '..name end 

When you use the greeter variable, its value is nil , because its value is set only on the next line.

But if you want your "main" program on top and functions below, there is a simple way to achieve this: create a "main" function and name it last at the bottom. By the time the function is called, all functions will be set to the corresponding global variables:

 -- start of program, your main code at the top of source code function main() local name = readName() local message = greeter(name) print(message) end -- define the functions below main, but main is not called yet, -- so there will be no errors function readName() io.write('Your name? '); return io.read() end function greeter(name) return 'Hello, ' .. name end -- call main here, all the functions have been assigned, -- so this will run without any problems main() 
+3
source

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


All Articles