I'm trying to wrap functions with a function for protection purpose.
My Code:
function Lib()
function self:foo(x, y) return x+y end
function self:goo(x, y) return x-y end
end
print(Lib():foo(3, 2))
I expect to get 5 but I get the following error.
attempt to index a nil value (global 'self')
What is wrong with my code and how to fix this?
ADDED: Can anyone compare this with using Lib = {} instead? I'm considering to wrap functions with a function since writing self: is easier to maintain than writing Lib. which can be changed later. I wonder if my idea makes sense.
EDITED: Okay, I just found out this works.
function Lib()
function foo(x, y) return x+y end
function goo(x, y) return x-y end
return self
end
Lib()
print(foo(3, 2))
But I don't understand why. Can't functions inside a function be protected?
P.S: I'm sorry if my question is stupid.