LuaLS / LuaLS/lua-language-server
[Feature request] Implicit casts for common conversions
还没有人认领这个 Issue。
- 主要语言
- Lua
- 星标
- 4.4k
- 派生
- 442
- PR 合并指标
- 30 天内没有已合并 PR
描述
I got some code for parsing command line flags/user inputs that commonly uses patterns like this
---@return string ...
local function strsplit(str, sep) end
local a, b, c = strsplit(userinput, ",")
b = tonumber(b) or 0 -- error: This variable is defined as type `string`. Cannot convert its type to `number`
The fix is to define b as string|number in the declaration, but it's a bit cumbersome, especially if I just want b as a number:
local a,
---@type number|string
b, c = strsplit(userinput, ",")
So it would be nice if common type conversion patterns like this could be recognized automatically and either allow the cast or infer the type as string|number from the beginning.
Edit: See comment below, the implicit cast is already happening, this is really about type inference based on later assignments. Which may or may not be a good idea.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先追踪 issue 中描述的现有 implicit-cast 和 type-inference 行为,尤其要关注后续赋值如何影响较早的变量声明。定义预期的推断规则,并根据命令行解析示例对其进行验证;完成的标准是就行为达成一致,并有测试覆盖最终推断出的类型。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- lua
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100