LuaLS / LuaLS/lua-language-server
Feature Request: ability to overload the return value type
還沒有人認領這個 Issue。
- 主要語言
- Lua
- 星號
- 4.4k
- 分支
- 442
- PR 合併指標
- 30 天內沒有已合併 PR
描述
### How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
### Which OS are you using?
macOS
### What is the issue affecting?
Annotations
### Feature request
In my game's ECS bindings I have getter functions that work like this:
```
---@param id integer
---@return number? inv_mass
---@return number? restitution
function ecs.get_collision_body_values(id) end
local inv_mass, restitution = ecs.get_collision_body_values(entity_id)
```
This calls into a native ECS implementation that either pushes the 2 return values and returns with `return 2;` (2 return values) or if the return values doesn't exist, push nothing and `return 0;` (zero return values).
This can be used in my game code like this:
```
local function debug_body(id)
local inv_mass, restitution = ecs.get_collision_body_values(id)
if inv_mass == nil then -- collision_body component not set, do nothing
return
end
---@cast restitution number
-- ^^ have to cast restitution number? -> number to avoid warning
print(inv_mass, restitution)
end
```
Would be nice if there was a way to annotate an "all or nothing" multiple return value type. E.g., a function will either return multiple return values (e.g., not `number?` but `number`) or that it will return nothing. Or I guess more generally, the ability to somehow specify a union of different multivalue return types.
FWIW, it'd be possible to return "a bunch of values" or "nothing" by using a `table?` return type. However, I'm trying very hard not to allocate tables in my hot game's hot paths, thus I favor concrete types and multiple return.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先追蹤 language server 如何表示帶註解的多個回傳值,以及如何在呼叫點檢查這些回傳值。使用 issue 中的 ECS 範例作為要調查的行為,包括零值和雙值兩種情況。當受支援的 annotation 能夠表達 all-or-nothing 或合併的多值回傳,而不需要使用所示的 cast,並且兩種情況都有 diagnostics 覆蓋時,即視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- lua
- 領域
- devtools
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100