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 function parameter types when overriding the same-named class function in an instance of that class #2859

Merged
merged 4 commits into from
Sep 18, 2024

Conversation

tomlau10
Copy link
Contributor

@tomlau10 tomlau10 commented Sep 18, 2024

resolves #2158, #2569 (comment)

The problem

Sometimes we will want to define interface for overriding in instance object of that class, and retain the param types defined in the interface while overriding. But currently no way to do so as described in #2158 and #2569 (comment)

---@class A
local A = {}

---@return A
function A.new() end

---@param x number
function A:user_callback(x) end

local a = A.new()
function a:user_callback(x) end -- no type infer for `x`, also throwing `duplicate-set-field` warning

Proposed change

Detailed explanation here: #2569 (comment)

Expected Result

---@class A
local A = {}

---@return A
function A.new() end

---@param x number
function A:user_callback(x) end

local a = A.new()
function a:user_callback(x)
    -- x -> number
    -- and no more `duplicate-set-field` warning :)
end

Additional notes

  • The way that I modified duplicate-set-field diagnostic is allowing an instance variable to override any of its class function/method.
    i.e. all function/methods from a class can be overriden in variable of that type, without any duplicate-set-field warning.
  • This auto infer only works in a class variable / instance variable pair ✅
    i.e. it doesn't work when the namespace is just a global variable ❌
--- file a
global_namespace = {}

---@param x number
function global_namespace.user_callback(x) end

--- file b
function global_namespace.user_callback(x)
    -- this will not work, because this is not in `class/type` variable pair
end
  • I believe a @override will be definitely needed for that case, as otherwise no way to distinguish between setting duplicate field or really doing override 😕

中文版

  • 支持在 instance variable 下 override 其 class function 時做 auto param type infer
  • 同時忽略在這種 use case 下的 duplicate-set-field warning
  • 這個只針對 class/type variable 組合下的 override 場景 (例子見上邊)
    • 並不處理 define 成 global namespace 後再做 override 的使用場景
    • 該種 use case 似必需等待 @override 語法支持了,否則不能區分是出現 duplicate 還是真的想 override

@tomlau10 tomlau10 changed the title Infer function parameter types when overriding the same-named class function or method in a type variable Infer function parameter types when overriding the same-named class function in an instance of that class Sep 18, 2024
@sumneko sumneko merged commit 8f96025 into LuaLS:master Sep 18, 2024
11 checks passed
@sumneko
Copy link
Collaborator

sumneko commented Sep 18, 2024

Thank you!

@tomlau10 tomlau10 deleted the feat/param_infer_for_override branch September 19, 2024 02:50
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.

Add support for overriding functions
2 participants