LuaLS / LuaLS/lua-language-server
Type inferencing of filter style functions
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
Given:
---@generic T
---@param f fun(a: T)
---@param t table<any, T>
---@return T[] (table)
local function tbl_filter(f, t)
return t
end
---@type string[]
local s = {'a', 'b', 'c'}
local r1 = tbl_filter(function(a) end, s)
r1 is inferred as unknown[] because a is unknown despite s being known. However, could we infer a from s and force the typechecker to insist a is a string?
If I remove the type from a:
---@generic T
---@param f fun(a)
---@param t table<any, T>
---@return T[] (table)
local function tbl_filter2(f, t)
return t
end
local r2 = tbl_filter2(function(_) end, s)
r2 is now inferred as string[] because only s is used to infer the type.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two tbl_filter examples from the issue and compare the inferred callback parameter and result types. Trace the language server's generic-function and callback type-inference entry points; done means the first callback parameter is inferred as string from s, r1 is string[], and incompatible callback types are rejected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100