-
-
Notifications
You must be signed in to change notification settings - Fork 332
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
Support generic table #3042
Comments
is this a feature request? If this is just a question, then I don't think it's possible. 🤔 ---@class MyClass
local MyClass = {}
local Classes = {}
---@generic T
---@param className `T`
---@return T
function GetClass(className)
return Classes[className]
end
local c = GetClass("MyClass") --> c: MyClass
ps: but if this is a feature request, then you may keep it open. |
|
I think this is different from what requested in this issue 😕
|
However, I don't have a global namespace. Adding this is not difficult. |
@tomlau10 Yes this is a feature request. For now I've implemented it using a predefined set of types in the table |
Yeah~ I almost forget that you can have a class and define the fields of it. ---@class MyClass
local MyClass = {}
---@class YourClass
local YourClass = {}
---@class Classes
---@field MyClass MyClass
---@field YourClass YourClass
local Classes = {}
local c1 = Classes["MyClass"] --> c1: MyClass
local c2 = Classes["YourClass"] --> c2: YourClass
local c3 = Classes["HisClass"] --> c3: unknown An improved versionluals supports
---@class Classes
local Classes = {}
return Classes
---@class MyClass
local MyClass = {}
---@class Classes
---@field MyClass MyClass
-- luals now knows `Classes.MyClass` = MyClass type
return MyClass
---@class YourClass
local YourClass = {}
---@class Classes
---@field YourClass YourClass
-- luals now knows `Classes.YourClass` = YourClass type as well
return YourClass
local Classes = require "Classes"
local c1 = Classes["MyClass"] --> c1: MyClass
local c2 = Classes["YourClass"] --> c2: YourClass
local c3 = Classes["HisClass"] --> c3: unknown These workarounds might suit your need before this feature request is implemented. |
Hi,
Is it possible to create a generic table such that the returned type is the key as a type. For example:
In the above example, I expect c to have the type MyClass
The text was updated successfully, but these errors were encountered: