LuaLS / LuaLS/lua-language-server

[Feature Request] Required nullable arguments

Open
#2,070 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When calling Java methods from Kahlua, the number of arguments is used to determine the appropriate implementation to call. Because of this, arguments that can be `nil` are required in the function call for appropriate resolution. This is usually not a concern in Lua, but it could be.

In this example, both `f()` and `f(nil)` are appropriate, since the value of `x` defaults to `nil`.
```
---@param x integer?
local function f(x) end
```

In this example, however, the behaviors of `t.f()` and `t.f(nil)` are *not* equivalent.
(Admittedly, this is a contrived example. Nonetheless, it shows that this would not only be useful for strange cases like Kahlua's implementation.)
```
---@class Example
---@field f fun(x: integer?)
t = {
f = function(...)
assert(select('#', ...) == 1)
end
}
```

---

I don't feel strongly about the annotation that would be used to specify this, but `!` comes to mind:
```
---@class Example
---@field f fun(x: integer?!)
t = {
f = function(...)
assert(select('#', ...) == 1)
end
}
```

The `?!`/`!?` pair may be redundant, since non-nil argument types are always required. It would also be feasible to represent required but nil-able arguments with `!` alone.

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

No repository file or test is named. Start by locating the annotation handling and the argument-count resolution paths relevant to Lua functions and Java/Kahlua calls. Compare the proposed `?!`, `!?`, and `!` forms, then define coverage for omitted versus explicit `nil` arguments and confirm the intended syntax before implementation.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.