Skip to content
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

There seems to be no way to define a non-nil generic return value for a function #152

Open
pouwelsjochem opened this issue Feb 11, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@pouwelsjochem
Copy link
Contributor

Environment

Name Version
IDEA version 2022.3.1
Luanalysis version 1.4.0
OS MacOS

Lua

Name Setting
Language level 5.2

Type Safety

Name Setting
Strict nil checks ☑️
Unknown type (any) is indexable ☑️
Unknown type (any) is callabale ☑️

What are the steps to reproduce this issue?

Consider the following utility function:

---@generic T, R
---@param _array T[]
---@param _mapperFunction fun(obj:T, i:number):R
function array.mapAndFilter(_array, _mapperFunction)
    local mappedArray = {} ---@type R[]
    for i = 1, #_array, 1 do
        local mappedValue = _mapperFunction(_array[i], i)
        if mappedValue ~= nil then
            mappedArray[#mappedArray + 1] = --[[---@not nil]] mappedValue
        end
    end
    return mappedArray
end

What happens?

Calling this function effectively returns an array of R where R is never nil. At the moment there seems to be no way to let Luanalysis deduce R can never be nil. I think defining the return value of _mapperFunction as (R|nil) could be a nice way to say R can never be nil.

@Benjamin-Dobell Benjamin-Dobell added the enhancement New feature or request label Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants