LuaLS / LuaLS/lua-language-server

Feature Request: ability to overload the return value type

Open
#3,424 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

### 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the language server represents annotated multiple return values and checks them at call sites. Use the ECS example in the issue as the behavior to investigate, including the zero-value and two-value cases. Done means a supported annotation can express all-or-nothing or unioned multivalue returns without requiring the shown cast, with diagnostics covered for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.