Skip to content

Conversation

lizho
Copy link
Contributor

@lizho lizho commented Jan 30, 2024

Support annotation of tuples by using square brackets.

---@type [string, integer]

@sumneko
Copy link
Collaborator

sumneko commented Feb 1, 2024

Thank you very much for your PR. I'd be happy to add this feature. However, based on my past experience, I suggest that you first enable this feature locally, use it in the actual project for about a week, confirm there are no obvious issues, and then let me know to merge. I've been quite busy with work lately and don't have much time to test this feature.

@lizho
Copy link
Contributor Author

lizho commented Feb 1, 2024

It has worked for a while now.

In fact, I'm working on another feature about variadic generic annotations in a way kind of similar to C++.

For example,

---@generic ...T
---@parameter ... T...
---@return [T...]
function tuple(...)
end

local t = tuple(1, "2", false) ---> `t` is the same as marked as @type [integer, string, boolean]

Furthermore, a more complex use is,

---@generic ...T
---@parameter ... T[]...
---@return fun(): T...
function zip(...)
end

---@type integer[]
local a
---@type string[]
local b
---@type table[]
local c

for i, s, t in zip(a, b, c) do
---[[
    in which,
    `i` -> integer,
    `s` -> string,
    `t` -> table
---]]
end

@sumneko
Copy link
Collaborator

sumneko commented Feb 19, 2024

Generics are very complex, and the current implementation is not quite right. I suggest you not to put too much effort into it for now.

@eigenbom
Copy link

eigenbom commented Jul 7, 2024

Thanks for implementing this! It helps me a lot with #1816

One thing I noticed is a conflict between subarrays and lua strings. The first form works, the second gets confused.

image

I'm not sure what a good resolution here is, but the second form is an invalid lua string (as the type annotations don't seem to allow multi-line params here..?) so it probably should be inferred as a tuple type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants