LuaLS / LuaLS/lua-language-server

Ability to mark default constructor names for resolving return type

Aperta
#2,331 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Lua
Stelle
4.4k
Fork
442
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Hi!

A request to infer return value for default returns when calling a defined constructor function. EmmyLua supports this using a setting:
image

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.

image

The return values of functions defined in fields from a set metatable cannot have it's return type defined however it seems.

image

image

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli scenari class.lua ed example.lua dell’issue, concentrandoti su come vengono risolti l’annotazione ---@class e la chiamata new(). Esamina la discussione correlata nell’issue #449 e l’alternativa @field descritta prima di scegliere un approccio. Il risultato finale dovrebbe includere la disponibilità inferita del costruttore e il tipo di ritorno corretto dell’istanza per l’esempio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
lua
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.