LuaLS / LuaLS/lua-language-server
Feature Request: ability to overload the return value type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Lua
- Sterne
- 4.4k
- Forks
- 442
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit nachzuverfolgen, wie der Sprachserver annotierte mehrere Rückgabewerte darstellt und sie an Aufrufstellen prüft. Verwende das ECS-Beispiel im Issue als das zu untersuchende Verhalten, einschließlich der Fälle mit null und zwei Werten. Die Aufgabe ist abgeschlossen, wenn eine unterstützte Annotation Rückgaben mit mehreren Werten als All-or-Nothing- oder vereinigte Rückgaben ausdrücken kann, ohne den gezeigten Cast zu erfordern, und die Diagnostik für beide Fälle abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- lua
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100