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

Proposal: Add @constructor Tag for Class Constructors in Lua LSP #3063

Open
ErickProgramer opened this issue Feb 7, 2025 · 1 comment
Open

Comments

@ErickProgramer
Copy link

ErickProgramer commented Feb 7, 2025

This Lua LSP could benefit from a new @constructor tag, which would be used when defining classes, especially with libraries. Currently, we have to do something like this:

---@class myCls
---@overload fun(a: integer, b: integer): myCls
local myCls = class "myCls"

---@param a integer
---@param b integer
function myCls:__init(a, b)
     self.a = a
     self.b = b
end
local instance = myCls(10, 20)
print(instance)

As you can see, we need to create an overload for the variable, which makes the typing process quite repetitive. To address this, we could introduce a new @constructor tag that would automatically handle this:

---@class myCls
local myCls = class "myCls" -- No need for an overload anymore.

-- Defining this method as the constructor:
---@constructor
---@param a integer
---@param b integer
function myCls:__init(a, b)
     self.a = a
     self.b = b
end
local instance = myCls(10, 20) -- Type checking would work correctly and show the constructor parameters.
print(instance)

This approach would make the code cleaner and more intuitive, reducing the need for repetitive annotations and improving the overall developer experience.

@ErickProgramer ErickProgramer changed the title @constructor tag Proposal: Add @constructor Tag for Class Constructors in Lua LSP Feb 7, 2025
@tomlau10
Copy link
Contributor

There has been feature requested before: #449
And the @overload is the compromised result: #449 (comment)


I am not maintainer, but I think there are some difficulties to implement the @constructor 😕 :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants