LuaLS / LuaLS/lua-language-server
@overload not as expected check param in class instance
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Lua
- Sterne
- 4.4k
- Forks
- 442
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Windows
What is the issue affecting?
Annotations
Expected Behaviour
当一个类通过@overload定义了__call元方法。
当它的一个实例以__call的形式被调用,应该检测参数是否正确。
When a class defines a __call metamethod via @overload.
When one of its instances is called as __call, the argument should be checked to see if it is correct.
Actual Behaviour
被正确地识别为了一个函数,但是没有检测其参数是否正确。
Is correctly identified as a function, but its arguments are not checked for correctness.
Reproduction steps
---@class Callable
---@overload fun(tbl:table)
local Callable = {}
Callable.__index = Callable
---@return Callable
function Callable:extend()
return setmetatable({ __index = self, __call = self.__call }, self)
return
function Callable:__call(tbl)
for key, value in pairs(tbl) do
print(key)
end
end
-- other lua file
local instance = { }
instance.callable = Callable:extend()
instance:callable { a=1, b=2 } -- A warning should be thrown here because `instance` is passed in as the first argument (self).
instance.callable { a=1, b=2 } -- Correct call.
Additional Notes
一个临时的解决方法:再次进行类型的声明。
Temporary workaround: Type declaration again.
local instance = { }
---@overload fun(tbl:table)
instance.callable = Callable:extend() --[[@as function]]
instance:callable { a=1, b=2 } -- A warning.
instance.callable { a=1, b=2 } -- Correct call.
我是在实现一个C#风格的Seter和Geter时发现的,我定义了一个Property类,子类会继承父类的Property。
为了使代码更加简洁,我定义了Property类的__call元方法,使得我可以以这样的风格进行声明:
I discovered this when implementing a C# style Seter and Geter, and I defined a Property class where subclasses inherit the Property of the parent class.
To make the code more concise, I define the __call metamethod of the Property class so that I can declare it in this style:
MyClass.property {
name = {
get = function(self)
return self._name
end,
set = function(self, value)
self._name = value
end,
}
}
Log File
No response
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der bereitgestellten Lua-Reproduktion und untersuche, wie der Sprachserver @overload bei __call-Metamethoden sowie Doppelpunkt- gegenüber Punkt-Aufrufen auflöst. Erledigt ist es, wenn der Doppelpunkt-Aufruf das zusätzliche self-Argument meldet, während der Punkt-Aufruf gültig bleibt, mit Abdeckung beider Formen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- lua
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100