Add __call (#1471)

* Add support for `__call` method

* Add small test for `__call` method

* Remove unecessary error
This commit is contained in:
MrMugame
2025-07-26 20:37:32 +02:00
committed by GitHub
parent 98cff15235
commit 6823e9c451
3 changed files with 56 additions and 21 deletions
+17
View File
@@ -258,6 +258,23 @@ assert(added[1] == 5 and added[2] == 7 and added[3] == 9)
local muliplied = t * 2
assert(muliplied[1] == 2 and muliplied[2] == 4 and muliplied[3] == 6)
-- __call Metamethod
local ts = {
foo = "ARG 2"
}
local mt = {}
function mt.__call(table, arg1)
assert(arg1 == "ARG 1")
assert(table.foo == "ARG 2")
return "return from metatable"
end
setmetatable(ts, mt)
assert(ts("ARG 1") == "return from metatable")
-- Let's try somethings lightly more complicated, like a deep comparison function implemented in Lua
function deepCompare(t1, t2)
if t1 == t2 then return true end