LuaLS / LuaLS/lua-language-server

[Feature request] Implicit casts for common conversions

Open
#3,027 4 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

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](https://github.com/LuaLS/lua-language-server/issues/3027#issuecomment-2572687841), 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.

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 the existing implicit-cast and type-inference behavior described in the issue, especially how later assignments affect an earlier variable declaration. Define the intended inference rules and verify them against the command-line parsing example; done requires an agreed behavior and tests covering the resulting inferred type.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.