lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

generic in the loop

Open RomanSpector opened this issue 1 year ago • 1 comments

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Annotations, Type Checking

Expected Behaviour

Снимок экрана 2024-03-24 160655

Actual Behaviour

Снимок экрана 2024-03-24 162116

Reproduction steps

---@generic T
---@param t T[]
---@return fun(): { first: integer, second: T }
local function test_function(t)
    local ending = #t
    local begining = 1

    local j = begining

    return function()
        if ending < j then
            return nil
        end

        local i = t[j]

        j = j + 1

        if i then return { first = j - 1, second = i } end
    end
end

---@type integer[]
local test_table = {}

local test_result = test_function(test_table)()

local test1 = test_result.second

for itr in test_function(test_table) do
    local test2 = itr.first;
    local test3 = itr.second;
end

Additional Notes

No response

Log File

No response

RomanSpector avatar Mar 24 '24 13:03 RomanSpector

If the type of the table is table<integer>, only the keys will be considered but not the values

---@generic T: table, K, V
---@param t T
---@return fun(table: table<K, V>): {first: K, second: V}
---@return T
local function test_function(t)
    local ending = #t
    local begining = 1

    local j = begining

    return function()
        if ending < j then
            return nil
        end

        local i = t[j]

        j = j + 1

        if i then return { first = j - 1, second = i } end
    end
end

---@type table<integer, integer>
local test_table = {}

for itr in test_function(test_table) do
    local test2 = itr.first;
    local test3 = itr.second;
end

MillhioreBT avatar Mar 24 '24 22:03 MillhioreBT