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

generic #1160

Closed
RomanSpector opened this issue May 19, 2022 · 2 comments
Closed

generic #1160

RomanSpector opened this issue May 19, 2022 · 2 comments
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) question User has a question

Comments

@RomanSpector
Copy link

RomanSpector commented May 19, 2022

---[Documentation](https://www.wowace.com/projects/libstub)
LibStub = {}

---@generic T
---@param major `T`
---@param minor number
---@return T library
function LibStub:NewLibrary(major, minor) end

---@generic T
---@param major `T`
---@param silent? boolean
---@return T library
function LibStub:GetLibrary(major, silent) end

first file

local lib = LibStub:NewLibrary("major", 1);


lib.table = {}
lib.number = 1;
lib.string = {}

function lib:func() end

1

second file

local lib = LibStub:GetLibrary("major", 1);

2

@sewbacca
Copy link
Contributor

sewbacca commented May 20, 2022

I encountered this issue too, however, as far as I understand, generic return values, are considered objects and not classes, thus any added fields, won't propagate back to the classes prototype. At least for the language server.
It would be great, if one could add classes implicitly, I don't know however, how hard this is to implement.
Here is a workarround: Just add ---@class major above the LibStub:NewLibrary() call

---@class major
local lib = LibStub:NewLibrary("major", 1);


lib.table = {}
lib.number = 1;
lib.string = {}

function lib:func() end

Now, anywhere, where is LibStub:GetLibrary("major", 1); called, you will get the completion info.

P.S. A more precise issue title would be beneficial, e.g. "Generically created classes, won't work"

Edit: A probably related issue: #1161

@sumneko sumneko added enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) question User has a question and removed enhancement New feature or request labels May 25, 2022
@sumneko
Copy link
Collaborator

sumneko commented May 25, 2022

You must use ---@class to declare the structure of the class to avoid accidental injection of fields (and provide correct warnings in the future)

@sumneko sumneko closed this as completed Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) question User has a question
Projects
None yet
Development

No branches or pull requests

3 participants