LuaLS / LuaLS/lua-language-server
Best way to implement something like an abstract base class?
还没有人认领这个 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 :(
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
复现 issue #2453 中带注解的 Lua 示例,并检查 language server 如何处理泛型返回值、继承的方法以及用户添加的字段。完成标准是:被重写的 OnClick 参数被推断为 number,而用户定义的方法(例如 myHelperFunction)不会产生注入字段诊断。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- lua
- 领域
- developer-experience, devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100