Say I declare a lua function using the : operator like so:
function ClassName:myFunc( stuff )
--do stuff
end
And then say I store that function in a table like so:
someTable = {
ClassName.myFunc,
someGlobalFunc,
}
And then, say I have another function that goes through the table and attempts to call the given functions.
function ClassName:callStuffInThisTable(table)
-- I go through the table, which might be someTable above, and call all the functions
end
My question is, how do I know if a function in the table is owned by ClassName, so that I can call it using self?
You don’t. At least, Lua isn’t going to tell you.
function ClassName:myFunc( stuff )is just syntactic sugar as far as Lua is concerned. It’s no different from this:ClassName.myFunc = function (self, stuff). The functions are equivalent.Likewise for the : call syntax,
ClassName:myFunc(stuff)is semantically equivalent toClassName.myFunc(ClassName, stuff).It is up to you to know what your functions are and what they do. This requires coding discipline. If you have a list of functions that you need to call in a loop, then they should be designed to be called with the same parameters.
There are two ways to do this. One way is to make all of the functions “class functions”:
This way, the
selfparameter is ignored. Obviously, you could create a special function table object that has functions for inserting “global” functions into the table that will automatically generate the wrapper:Note: there is a difference between these, assuming “someGlobalFunc” is actually a member of the global table (rather than a
local). This version will take the value that_G["someGlobalFunc"]currently has, just as your original code does. However, the first version takes the value that it has at the time it is called, which may be a different function from the one it was at the timesomeTablewas created.So this version is safer.
Alternatively, you can make it so that any “class functions” in the table are explicitly bound to an object instance:
BTW, in general, if you declare a function using
:syntax, you’re supposed to use that function that way, viainstance:myFunc(...). Obviously it’s just a Lua function like any other, so you can do what you like. But misuse can make understanding what’s going on harder.Lua affords you a lot of power. But you’re still required to exercise judgement and discipline when coding. Lua will not save you from yourself (entirely).