Open 4lph4-Ph4un opened 2 days ago
With your code, I am getting c: integer|boolean
and d: unknown
table.unpack
I guess this is due to how table.unpack
is annotated: https://github.com/LuaLS/lua-language-server/blob/6ba0c9362ebfec8a668d452b0a752216fd52d543/meta/template/table.lua#L58-L66
T[]
to match the list. Although T
is a generic type, is can only match one type (or a union type) => homogeneous arraytuple
is an array of integer|boolean
=> so it matches T
to integer|boolean
T ...
(variadic return) but unfortunately, the generic support of luals is quite limited as seems currently no way to return variadic generic type, so we are getting d: unknown
...In this PR https://github.com/LuaLS/lua-language-server/pull/2587, @lewis6991 tried with with a "hack" to annotate unpack
table.unpack
, however, seems not working well ☹️
---@generic T1, T2, T3, T4, T5, T6, T7, T8, T9
---@param list {[1]: T1, [2]: T2, [3]: T3, [4]: T4, [5]: T5, [6]: T6, [7]: T7, [8]: T8, [9]: T9 }
---@return T1, T2, T3, T4, T5, T6, T7, T8, T9
---@nodiscard
function table.unpack(list) end
---@alias my_tuple { [1]: integer, [2]: boolean }
---@type my_tuple local tuple = {1, true}
local a, b, c = table.unpack(tuple) -- a: boolean|integer, b: boolean|integer, c: integer (extra one??)
local a, b, c = unpack(tuple) -- same result as above
- although it can provide types for the return value, none of them are actually correct 😇
- I am using v3.10.6, maybe this hack doesn't work anymore after some recent change in luals ☹️
---
> I'm not sure if this is a feature that is even considered
I don't know if it has been considered, but definitely it is currently not supported.
How are you using the lua-language-server?
Other
Which OS are you using?
Windows
What is the issue affecting?
Diagnostics/Syntax Checking
Expected Behaviour
I might be wrong here, but I assume this should work? Language server should detect the types from the defined tuple:
Actual Behaviour
Reproduction steps
unpack
to two variablesAdditional Notes
I'm not sure if this is a feature that is even considered, but since I do use unpack sometimes to destructure tuples into their distinct variables, I feel this would be a good feature to have.
Log File
No response