Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] generics in overload #723

Open
lua-rocks opened this issue Oct 10, 2021 · 5 comments
Open

[Feature request] generics in overload #723

lua-rocks opened this issue Oct 10, 2021 · 5 comments
Labels
enhancement New feature or request feat/generic Related to generic emulation feature feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)

Comments

@lua-rocks
Copy link

lua-rocks commented Oct 10, 2021

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 🤯

@sumneko sumneko added feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) enhancement New feature or request labels Oct 11, 2021
@Miqueas
Copy link

Miqueas commented Oct 13, 2021

Interesting... Aside of the EmmyLua annotation, how do you achieve overloading in Lua? 👀

@lua-rocks
Copy link
Author

@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.

@Miqueas
Copy link

Miqueas commented Oct 14, 2021

@lua-rocks awesome, thanks!

@carsakiller carsakiller added the feat/generic Related to generic emulation feature label Jan 30, 2023
@Mayron
Copy link

Mayron commented Apr 12, 2023

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.

@distantforest1
Copy link

distantforest1 commented Aug 22, 2023

Just want to add my +1 to this as well. Just ran into this issue. Would be great to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feat/generic Related to generic emulation feature feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Projects
None yet
Development

No branches or pull requests

6 participants
  翻译: