LuaLS / LuaLS/lua-language-server

Best way to implement something like an abstract base class?

未關閉
#2,453 3 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Lua
星號
4.4k
分支
442
PR 合併指標
30 天內沒有已合併 PR

描述

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 :(

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

重現 issue #2453 中附有註解的 Lua 範例,並檢查 language server 如何處理泛型回傳值、繼承的方法以及使用者新增的欄位。完成標準是:被覆寫的 OnClick 參數會被推斷為 number,而使用者定義的方法(例如 myHelperFunction)不會產生注入欄位診斷。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
lua
領域
developer-experience, devtools
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。