Support for capturing function names
Similar to how you can capture class names in generics and annotate a function to then return that class it would be cool if you could do the same for functions. In a modloader we have a hooking function like this:
hook(object, "function_name", function(...) end)
which makes a function run after the hooked function on an object with the same parameters as the original function.
I understand if thats too much effort to implement but it would be nice if it was somehow possible to annotate the second argument of the hook function to be the name of a function on object.
I think you want:
checking:
But LuaLS does not support this.
I think OP wants the callback function to have the same function signature as type[key] 🤔
Maybe just ignore the concept of passing object, "function_name" at the moment, simply treat it as:
---@param a integer
---@param b string
function object.function_name(a, b) end
hook(object.function_name, function (a, b)
-- here wants auto infer `a: integer, b: string` ?
-- i.e. treat this callback function the same signature as `object.function_name`
end)
There seems to be more considerations as well:
- if it is not function but a method =>
function object:function_name(a, b) - which is actually a syntactic sugar for
function object.function_name(self, a, b) - does the callback function's 1st param be
selfof justa?
There's basically two thoughts here. First, when using the hook function, the function name should be suggested based on the object specified in the first argument. Second, the callback function should keep the signature of object.function_name to help with typing of parameters as well as instance methods and members in case of a method hook (in which case, yes, self should be the first parameter).
A keyof annotation would already go a long way I think but I'm not sure how much effort this would be.