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

Infer overload based on argument values #1898

Closed
sapphyrus opened this issue Feb 10, 2023 · 2 comments
Closed

Infer overload based on argument values #1898

sapphyrus opened this issue Feb 10, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@sapphyrus
Copy link

Currently, multiple "overloaded" annotations for the same function seem to always be combined into one union, instead of selecting the overload based on the actual, known argument values. For example:

---@meta

---@class event_a
---@field prop_a integer
local event_a = {}

---@class event_b
---@field prop_b integer
local event_b = {}

---@param event_name "a"
---@param callback fun(evt: event_a)
function register_callback(event_name, callback) end

---@param event_name "b"
---@param callback fun(evt: event_b)
function register_callback(event_name, callback) end

The different values for the event_name parameter could be used to deduce the callback type, and therefore the type of the evt parameter. For example when used like this, the type of evt should be known to be event_b:

register_callback("a", function(evt)
    print(evt.prop_a) -- valid
    print(evt.prop_b) -- not valid (Undefined field)
end)
@sumneko sumneko added the enhancement New feature or request label Feb 10, 2023
@carsakiller
Copy link
Collaborator

There are a bunch of issues referencing this. I think the title of this issue best suits the issue. However, the use case in this issue is almost the exact same as in #1456, which discusses changing the way things are overloaded.

Similar:
#1343
#1410

@carsakiller
Copy link
Collaborator

This does indeed seem to be working now.

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