LuaLS / LuaLS/lua-language-server

Best way to implement something like an abstract base class?

Đang mở
#2,453 3 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Lua
Star
4.4k
Fork
442
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

I'm working on a framework where users of it implement a kind of module that mixes their provided logic with some optional event handlers.

Here is a simple example

-- provided by framework
---@class Base
---@field OnClick fun(self: Base, x: number, y: number)
local base = {}

---@generic T: Base
---@param name `T`
---@return T
function New(name)
	return setmetatable({}, {__index = base})
end

-- usage of framework by user

---@class MyImpl: Base
local foo = New("MyImpl")

function foo:OnClick(x, y) -- x and y are inferred as any instead of number :(
end

function foo:myHelperFunction()
end

So I expect the user to implement OnClick and fill it with their logic. My goal here is that the user automatically gets the correct types on the parameters for foo:OnClick. Here in this simple example it's just two numbers, but my real app has like > 10 such methods and some of these take complex types as parameters.

With the code above it doesn't know anything about the parameters in OnClick and shows them as any. Maybe I got something wrong with the generics, but even without all the fancy generic stuff (just @return base on New) it's the same problem.

The only work-around I found was to not use @class on the user-defined class and not define a generic, this makes the parameters on the OnCLick function definition correct! However, it now triggers injected-field on all user-defined values not defined in the base class like the myHelperFunction method :(

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ Lua có chú thích từ issue #2453 và kiểm tra cách language server xử lý các giá trị trả về generic, các phương thức được kế thừa và các trường do người dùng thêm vào. Được xem là hoàn tất khi các tham số OnClick bị ghi đè được suy luận là number, trong khi các phương thức do người dùng định nghĩa như myHelperFunction không tạo ra chẩn đoán về trường được inject.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
lua
Lĩnh vực
developer-experience, devtools
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.