I am just curious if there is a difference between these two different types of functions.
function PrintHello()
return print("Hello")
end
and
PrintHello = function()
return print("Hello")
Beside that you're missing an end in the second function both snippets are equivalent.
According to the Lua 5.4 Reference Manual 3.4.11 - Function Definitions
function PrintHello ()
print("Hello")
end
translates to
PrintHello = function ()
print("Hello")
end
As you'll also find in the manual there is a difference for local functions.
local function a() end
translates to
local function a;
a = function () end
This allows a to reference itself for example in a recursive call.
Instead of return print("Hello") simply write print("Hello"). print has no return value.
function PrintHello ()
print("Hello")
end
Related
I have the following scenario in which the position of code shall not change. How to modify this code to fix the error without moving the function and table variable. I am a Lua newbie, just 4 days
function a()
print("function a");
end
ftable = {
["a"] = a,
["b"] = b
};
function b()
print("function b");
end
ftable["a"](); -- prints 'function a'
ftable["b"](); -- attempt to call field 'b' (a nil value)
Update : Using the following mechanism its possible to do this.
function a()
print("function a");
end
ftable = {
["a"] = a,
["b"] = "b"
};
function b()
print("function b");
end
ftable["a"](); -- prints 'function a'
_G[ftable["b"]]();
Lua's table declaration and function definition (and function calling syntax) is very flexible. You can use the field key as an identifier instead of the index syntax, provided that the key is a string and is also a valid identifier ({ a = a } and ftable.a()). This seems to be the case with your a and b keys. You can also make the field assignment with the function definition statement (function ftable.b…).
function a()
print("function a")
end
ftable = {
a = a
}
function ftable.b()
print("function b")
end
ftable.a() -- prints 'function a'
ftable.b() -- prints 'function b'
Of course, you could also move "function a" down and code it like "function b". that would leave ftable = { } at the top.
One difference from your code. The global variable b is no longer set to the function. In most cases, this would be considered an advantage.
I would also like to re-iterate what #lhf said,
There are no declarations in Lua, only definitions.
A function definition (in the general case) is an expression. When evaluated, it produces a function value. Various types of statements can assign the value to a variable or field, as does function ftable.b()….
You can't do that. There are no declarations in Lua, only definitions. You need to define variables before using them.
What you can do is to register the names of the functions in ftable and then fix the values before using them:
ftable = {
["a"] = true,
["b"] = true,
}
...
for k,v in pairs(ftable) do ftable[k]=_G[k] end
This code assumes that your functions are defined globally.
I have this Lua code to initialize a table:
table = {
a = 1;
b = myfunc();
c = function () <some code> end;
}
After this table.c has type function and I have to use table.c()
in a print statement with the .. operator to get the result. But I would like to just use table.c instead.
Is there a way that I can get the return value of the function assigned to table.c so the type is not function without having to define a function outside of the table?
If you wanted table.c to contain the return value of the function, then you should have assigned it the return value of the function. You instead assigned it the function itself.
To get the return value of a function, you must call that function. It's really no different from b. myfunc is a function; myfunc() is calling that function and storing its return value.
But, due to Lua's grammar, calling a function that you are defining requires that you need to wrap the function constructing expression in (), then call it:
c = (function () <some code> end)();
This of course will only contain the value of that function at the time that the table is constructed.
Is there any difference between
local splitPathFileExtension = function (res)
end
and
function splitPathFileExtension(res)
end
? I understand in the first case this function is anonymous but this is the only difference?
They are almost exactly the same thing (other than the fact that you've specified the first function as local and not the second one.)
See the manual on function definitions:
The statement
function f () body end
corresponds to
f = function () body end
The statement
function t.a.b.c.f () body end
translates to
t.a.b.c.f = function () body end
The statement
local function f () body end
translates to
local f; f = function () body end
not to
local f = function () body end
(This only makes a difference when the body of the function contains references to f.)
All functions are anonymous, they don't have names. A function definition is in fact an assignment statement that creates a value of type function and assigns it to a variable.
The second code is syntactic sugar that's equivalent to:
splitPathFileExtension = function (res) end
So, other than the first is local while the second is global, there's no difference between the two ways of function definition.
so i have a lua file analogous to this:
x = { __index = x}
constructor = function()
local o = {}
return setmetatable(o,x)
end
function x:print()
print("hello world")
end
i type the following into the interpretr:
dofile "file.lua"
a = constructor()
a:print() --error attempt to call method 'print' (a nil value)
dofile "file.lua"
a = constructor()
a:print() -- hello world
the method works the second time i import the file but not the first. why is this?
I have tried changing the order (putting the constructor function last) and it was the same.
The first time x is nil. It gets defined and then used the second time.
You need to write x = {}; x.__index = x.
I know this question seems simple, but I want to know the difference between two ways of creating functions in Lua:
local myFunction = function()
--code code code
end
Or doing this
local function myFunction()
--code code code
end
The difference happens if the function is recursive. In the first case, the "function" name is not yet in scope inside the function body so any recursive calls actually refer to whatever the version of "myFunction" that was in scope before you defined your local variable (most of the times this meas an empty global variable).
fac = "oldvalue"
local fac = function()
print(fac) --prints a string
end
To be able to write recursive functions with the assignment pattern, one thing you can do is predeclare the variable:
local myFunction
myFunction = function()
-- ...
end
Predeclaring variables also happens to be the only way to define a pair of mutually recursive local functions:
local even, odd
even = function(n) if n == 0 then return true else return odd(n-1) end end
odd = function(n) if n == 0 then return false else return even(n-1) end end
The difference is that according to the manual:
The statement
local function f () body end
translates to
local f; f = function () body end
not to
local f = function () body end
(This only makes a difference when the body of the function contains references to f.)
The main reason is that the scope of a variable (where the variable is visible) starts AFTER the local statement, and if a function was recursive, it would not reference itself, but a previous local or a global named f.