Skip to content

Can't infer the return type of map #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skanev opened this issue May 18, 2022 · 1 comment
Closed

Can't infer the return type of map #1153

skanev opened this issue May 18, 2022 · 1 comment
Labels
enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Milestone

Comments

@skanev
Copy link

skanev commented May 18, 2022

Describe the bug

I'm writing a map function, but type inference doesn't work for it. Specifically, when you have map(list, fn) where fn is fun(item: A): B, the type inference cannot figure out what B is in the return type.

To Reproduce

Here's a snippet:

---@param n number
---@return string
local function as_string(n)
  return tostring(n)
end

---@generic A
---@generic B
---@param array A[]
---@param fn fun(item: A): B
---@return B[]
local function map(array, fn)
  local result = {}

  for _, item in ipairs(array) do
    table.insert(result, fn(item))
  end

  return result
end

---@type number[]
local numbers
numbers = { 1, 2, 3 }

-- expected to be string[], but is <B>[]
local strings = map(numbers, function(n) return as_string(n) end)

I'm using 3.2.3 in neovim, if that is relevant.

@sumneko sumneko added enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) labels May 24, 2022
@sumneko
Copy link
Collaborator

sumneko commented May 24, 2022

Need resolve generic by return type of param function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Projects
None yet
Development

No branches or pull requests

2 participants