-
-
Notifications
You must be signed in to change notification settings - Fork 357
[Feature request] generics in overload #723
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Interesting... Aside of the EmmyLua annotation, how do you achieve overloading in Lua? 👀 |
@Miqueas Basic overloading is achieved by checking the types of function arguments and returning a value according to the data obtained: ---@overload fun(v1:number, v2:number):number
---@overload fun(v1:string):string
local function demo(...)
local args = {...}
if #args == 2
and type(args[1]) == 'number'
and type(args[2]) == 'number' then
return args[1] + args[2]
elseif #args == 1
and type(args[1]) == 'string' then
return args[1]:upper()
end
error('wrong arguments', 2)
end
assert(demo(2, 2) == 4)
assert(demo('hello') == 'HELLO') A more complex version is demonstrated here. |
@lua-rocks awesome, thanks! |
Just discovered this problem as well. Looking forward to being able to use both of these together. I don't think any additional annotation should be required. Overload just needs to accept the already specified generic annotation. |
Just want to add my +1 to this as well. Just ran into this issue. Would be great to have. |
+1 waiting for the
usage:
|
Hi @ZSaberLv0, if you are writing a
So in your case you can have something like this: ---@meta
---@class PropType
PropType = {}
---@class Base
---@overload fun(): Base
Base = {}
---@return PropType
function Base:baseProp() end
---@generic T
---@param self T
---@param v PropType
---@return T
function Base:baseProp(v) end
---@class Child: Base
---@overload fun(): Child
Child = {}
function Child:childProp(v) end However I don't know why the above doesn't work 😕 it returns local obj = Child()
local r = obj:baseProp(xxx) --> r: unknown Only if I remove the The above is tested in edit
edit2I think I have found a fix for this regression issue. Although I don't fully understand how the fix works, at least after patching then my above code snippet works again 😂 I will open a PR for it.
-- clear node caches of args to allow recomputation with the type narrowed call
for _, arg in ipairs(call.args) do
if vm.getNode(arg) then
vm.setNode(arg, vm.createNode(), true)
end
end
for n in newNode:eachObject() do
if n.type == 'function'
or n.type == 'doc.type.function' then
for i, arg in ipairs(call.args) do
if vm.getNode(arg) and n.args[i] then
vm.setNode(arg, vm.compileNode(n.args[i]))
end
end
end
end
|
Uh oh!
There was an error while loading. Please reload this page.
As far as I know, it is not possible to use
@generic
in@overload
?I have a table
Object
that has a__call
metamethod, so class Object can be used as function:Object()
==Object:new()
... And I want to overload class Object with function
new()
. This function use generics 🤯The text was updated successfully, but these errors were encountered: