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

vararg overload issue #2277

Open
GiuseppeIII opened this issue Aug 20, 2023 · 3 comments
Open

vararg overload issue #2277

GiuseppeIII opened this issue Aug 20, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@GiuseppeIII
Copy link

If you have a function which takes in only a vararg and you only overload it, the input type is as expected just each of the overloads:

---@overload fun(param1: "test", param2: string)
---@overload fun(param1: "test2", param2: number)
---@overload fun(param1: "test3", param2: boolean, param3: number)
local function test(...)
end

test()
Screenshot_2

However, if you have a param before the vararg, the input type always becomes param, ...any:

---@overload fun(param1: "test", param2: string)
---@overload fun(param1: "test2", param2: number)
---@overload fun(param1: "test3", param2: boolean, param3: number)
local function test(param1, ...)
end

test()
Screenshot_3
@carsakiller
Copy link
Collaborator

I'm not sure that this is an appropriate use case of variable arguments. ... is typically used when expecting a conceivable limitless number of parameters of a specific type. I think in this case you would just want to use the following:

---@param one "one"
---@param two string
---@overload fun(one: "two", two: number)
---@overload fun(one: "three", two: boolean, three: number)
local function test(one, two, three) end

test()

The types of the overloads likely won't be narrowed correctly, but that issue is being tracked in #1456.

@GiuseppeIII
Copy link
Author

GiuseppeIII commented Aug 20, 2023

I think my example might have confused how I am trying to use it here. I am trying to use it in a situation like

---@overload fun(event: "test", event_test_param: string)
---@overload fun(event: "test2", event_test2_param: number)
---@overload fun(event: "test3", event_test3_param1: boolean, event_test3_param2: number)
local function send_event(event, ...)
end

Where, the ... can be any number of args in the actual function itself but I want it to be constrained when using by the actual parameters of the given functions. (It works like this for a vararg by itself (see the first example) just not when you have param, vararg)

@carsakiller
Copy link
Collaborator

I see, so it is using variable arguments, just that there are a few cases where the passed parameters should be of certain types.

@sumneko sumneko added the enhancement New feature or request label Aug 21, 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

3 participants