Whenever you write methods under metatables and so-forth. You’re writing it with an implicit self as first parameter if the : is used for sugar syntaxing. Therefore:
Declaring
function module.foo(self, params)
-- Declaring with a dot operator instead
end
function module:foo(params)
-- Equivalent to above
end
I assume the real problem here is how to typecheck/verify self, in order to figure out if the first variable is a “self” or not. A way that comes to mind is checking if the dictionary of the module and self have same key/value pairs(without directly comparing, because that will compare the table references and not give you the desired result).
Although if you follow the above you may want to add some exclusions to the mix, for example if you’re checking a property that can be modified through methods of your object you may want to check if the types of the value match(assuming you know the types) instead of the value itself. For example if I have a “Car” object with a “Speed” property, I would consider checking if the self provided has a “Speed” property of type number?(number or nil) instead of checking if the speed is a value like 25.
Basically instead of developing a way to detect self, you can develop a way to detect your object class, and if self doesn’t belong to it, assume it’s not a self variable.
This would work, but what if the method accepts additional arguments?
Edit: yes it does work with multiple arguments. I guess this might be the cleanest solution. I’d still prefer if I didn’t have to add this boilerplate code to every single method…