LuaLS / LuaLS/lua-language-server
Ability to mark default constructor names for resolving return type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Lua
- Sterne
- 4.4k
- Forks
- 442
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
Hi!
A request to infer return value for default returns when calling a defined constructor function. EmmyLua supports this using a setting:
Here it can infer that calling e.g. :new() on items marked with the ---@class annotation will be returning an instance of that class.
This is especially useful when constructing classes using metatables in a generic class system, adding interfaces or inheritance functionality but still be able to denote the return type of calling the constructor method.
Example:
#class.lua
local definitions = {}
local Class = {}
function Class.class(name)
local definitions = {}
local def = definitions[name]
if not def then
def = {}
def.__class_name = class_name
def.new = function(self, ...)
local instance = {}
if instance.construct then
instance:construct(...)
end
return instance
end
setmetatable(instance, def)
def[class_name] = def
end
return class_definition
end
Example of using the class creation method
#my_class.lua
local Class = require "path/class"
local MyClass = Class("MyClass")
function MyClass:construct()
self._member = 0
end
Example of instantiating an instance of the class and showing the issue:
#example.lua
local MyClass = require "path/my_class"
local instance = MyClass:new() -- this line cannot find the function new() or deduce the return/type of "instance"
Here the instance of class return value from the new() function cannot be inferred and I haven't found a way to annotate this using luadocs with generics, alias, overload or other methods. This issue is somewhat related to #449 but I feel this warrants a retake on the proposed solution unless I am missing something?
A different way could be to allow explicit descriptors of the @field annotation to specify return value types in class definitions e.g.
The return values of functions defined in fields from a set metatable cannot have it's return type defined however it seems.
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 den class.lua- und example.lua-Szenarien im Issue und konzentriere dich darauf, wie die Annotation ---@class und der Aufruf new() aufgelöst werden. Sieh dir die zugehörige Diskussion in issue #449 sowie die beschriebene Alternative @field an, bevor du einen Ansatz auswählst. Die fertige Umsetzung sollte die inferierte Verfügbarkeit des Konstruktors und den korrekten Rückgabetyp der Instanz für das Beispiel umfassen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- lua
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100