In Lua, how does a = Account:new{balance = 0} work or is it a typo? - lua

On the Lua website https://www.lua.org/pil/16.1.html , there is this section of code
function Account:new (o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end
a = Account:new{balance = 0}
a:deposit(100.00)
Shouldn't the line a = Account:new{balance = 0} be written as a = Account:new(balance = 0) with the brackets being replaced with parentheses because it is a function?

Account:new { balance = 0 } is syntactic sugar for Account:new({ balance = 0 }).
This is, if the only argument passed to a function is a table constructor, or string literal, then the parenthesis can be omitted.
The most common place you'll see this is in lines with require:
local env = require 'env'
This is discussed in §3.4.10 – Function Calls of the Lua Reference Manual:
A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string.

Related

Bind function with self in single line in Lua

I would like to have a function pointer (don't know what the term is in Lua) that has the self pointer baked in.
A concept example
x = {text = "hello there"}
function x.hello(self)
print(self.text)
end
--- This is where I'm stuck
function_pointer = ???
function_pointer() -- Expected behaviour is to call x:hello() but how
Is there any way possible to bake the self pointer into a function (pointer)? Like that?
Another (easy/lazy) way is to construct the table itself as a function for its content.
This is done with setmetatable() and the __call Metamethod.
x = setmetatable({text = "Hello there"}, {__call = function(self) print(self.text) end})
x() -- Hello there
-- A pointer will be...
fp = getmetatable(x).__call
fp(x) -- Hello There
PS: You are totally free to choose the Name for self
x = setmetatable({text = "Hello there"}, {__call = function(this) print(this.text) end})
...especially the 'Basic People' will using this ;-)
It is only the first Element in the Chain of Arguments...
local func = function(...) print(({...})[1].text) end -- Anonymous "self"
x = setmetatable({text = "Hello there"}, {__call = func})

Using a variable as arithmetic operator in Lua

I want to use a variable that references an arithmetic operator within an if statement expression as shown below:
str = { '>60', '>60', '>-60', '=0' }
del = 75
function decode_prog(var1, var2)
op = string.sub(var1, 1, 1)
vb = tonumber(string.sub(var1, 2, 3))
if var2 op vb then
print("condition met")
else
print('condition not meet')
end
end
for i = 1, #str do
decode_prog(str[i], del)
end
When the above code executes, it should either print "condition met" or "condition not met" based on the result of the operation, however I am instead receiving an error.
You cannot substitute a native Lua operator with a variable that references a function, the only way to go about what you are attempted to do is to create a set of functions within an associative array and set the index as a reference to the respective operation you want to conduct.
Looking at your list, you have a greater than (>) and equal to (=). We create a table for these operations that takes two parameters as follows.
local operators = {
[">"] = function(x, y) return x > y end,
["="] = function(x, y) return x == y end,
-- Add more operations as required.
}
You can then invoke the respective function from the decode_prog function by obtaining the operation character from the string, along with the numeric value itself - this is possible because you can obtain the function from the associative array where the index is the string of the operation we want to conduct.
local result = operators[op](var2, number)
This calls upon the operators array, uses the op to determine which index we need to go to for our appropriate operation, and returns the value.
Final Code:
str = { '>60', '>60', '>-60', '=0' }
del = 75
local operators = {
[">"] = function(x, y) return x > y end,
["="] = function(x, y) return x == y end,
}
function decode_prog(var1, var2)
local op = string.sub(var1, 1, 1) -- Fetch the arithmetic operator we intend to use.
local number = tonumber(string.sub(var1, 2)) -- Strip the operator from the number string and convert the result to a numeric value.
local result = operators[op](var2, number) -- Invoke the respective function from the operators table based on what character we see at position one.
if result then
print("condition met")
else
print('condition not meet')
end
end
for i = 1, #str do
decode_prog(str[i], del)
end
I can't make much sense of your code or what you want to achieve doing that but if could simply use load.
You build your expression as a string and run it. Of course you should take care of two character operators like >= which I did not and you should validate your input.
local str={'>60','>60','>-60','=0'}
local del=75
function decode_prog(var1, var2)
local operator = var1:sub(1,1):gsub("=", "==")
local expr = string.format("return %d %s %s", var2,operator, var1:sub(2))
print(string.format("condition %smet", load(expr)() and "" or "not "))
end
for i,v in ipairs(str) do
decode_prog(v, del)
end
A very simple way would be to add a condition for each supported operator:
function decode_prog(var1, var2)
op = string.sub(var1, 1, 1)
vb = tonumber(string.sub(var1, 2)) --remove the last argument and use tonumber()
if vb == nil then return end --if the string does not contain number
if (op == ">" and var2 > vb) or (op == "=" and var2 == vb) --[[add more conditions here]] then
print("condition met")
else
print("condition not met")
end
end
I changed the vb=string.sub(var1,2,3) line too.
This form vb = tonumber(string.sub(var1, 2)) will allow use of numbers that have any number of digits and added tonumber() which will allow us to catch not-a-number errors when comparison would probably fail.
Then I added a logic to determine what the operator is and if the condition is met.
Operator limitations:
This will only work with operators that are one character and operator such as >= will not be possible unless you use a different character for it. ≥ will not play nicely, since it is multiple characters.

How to have function as field value in a lua table, when the function is declared at a later point?

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.

Is it possible to define functions globally in Lua's table

How to define functions into lua's table? I try this code but doesn't work.
I want to use table:myfunc().
local myfunc
myfunc = function(t)
local sum = 0
for _, n in ipairs(t) do
sum = sum + n
end
return sum
end
mytable = {
1,2,3,4,5,6,7,8,9
}
print(myfunc(mytable)) -- 45
I think myfunc has not problem.
table.myfunc = myfunc
print(mytable:myfunc())
-- lua: main.lua:18: attempt to call method 'myfunc' (a nil value)
-- stack traceback:
-- main.lua:18: in main chunk
-- [C]: ?
print(mytable) shows table: 0x9874b0, but it is not defined function to the table?
mytable.myfunc = myfunc
print(mytable:myfunc()) -- 45
This worked. Is it the only prefer way to do this?
there are plenty of ways to define a function within a table.
I think the problem with your first code is that you have this:
table.myfunc = myfunc
where it should be:
mytable.myfunc = myfunc
In your case mytable.myfunc is nil as you never assigned a value to it
You could just write
local mytable = {}
function mytable.myfunction()end
or
mytable.myfunction = function()end
or
mytable["myfunction"] = function()end
or define myfunc separately and assign it to mytable.myfunc later like you did
If you want to access other table members from your function I recommend defining the function like that:
function mytable:myfunc()end
which is syntactic sugar for
function mytable.myfunc(self)end
If you do so you can access mytable's member trough the keyword self
In your case it would end up like:
function mytable:myfunc()
local sum = 0
for _, n in ipairs(self) do
sum = sum + n
end
return sum
end
So you don't need the function parameter t anymore and you can run the desired mytable:myfunc()
Otherwise you would have to write mytable:myfunc(mytable).
table.myfunc = myfunc
print(mytable:myfunc())
This cannot work, because mytable has no member called myfunc, table does.
you would either have to write
table.myfunc = myfunc -- assign myfunc to TABLE (not the same as mytable)
print(table.myfunc(mytable)) -- as with most other functions in table
or
mytable.myfunc = myfunc -- assign myfunc to MYTABLE
print(mytable:myfunc()) -- syntactic sugar for print(mytable.myfunc(mytable))
also, you can just define the function as either
function mytable:myfunc() -- syntactic sugar for mytable.myfunc(self)
-- do something
-- use "self" to access the actual table
end
or
function table.myfunc(t)
-- do something
end

Lua class not working

I have a simple class implementation in Lua.
test = {}
test.__index = test
function test:new()
local o = {}
setmetatable(o, self)
return o
end
function test:setName(name)
self.name = name
print name
end
local name = test:new()
name:setName("hello")
I keep getting this error when I run it:
lua: test.lua:12: '=' expected near 'name'
I not sure what or why this happening, any help would be greatly appreciated.
Change print name to print(name). print is just a regular function and function calls need parentheses unless they are called with a single argument that is either a string literal or a table literal.

Resources