LuaLS / LuaLS/lua-language-server

Ability to mark default constructor names for resolving return type

Ouverte
#2,331 0 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
Lua
Étoiles
4.4k
Forks
442
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Hi!

A request to infer return value for default returns when calling a defined constructor function. EmmyLua supports this using a setting:
![image](https://github.com/LuaLS/lua-language-server/assets/4696942/662ffc29-e3ab-40e2-8592-4ff8d546a5ce)

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](https://github.com/LuaLS/lua-language-server/assets/4696942/093f448d-18f3-472c-a275-a69be639593b)

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

![image](https://github.com/LuaLS/lua-language-server/assets/4696942/688f56f3-1700-474f-86da-a889f2055446)

![image](https://github.com/LuaLS/lua-language-server/assets/4696942/4f94b87c-bacc-429f-b588-e9730515beca)

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Start with the class.lua and example.lua scenarios in the issue, focusing on how the `---@class` annotation and `new()` call are resolved. Review the related discussion in issue #449 and the described `@field` alternative before choosing an approach. Done should include inferred constructor availability and the correct instance return type for the example.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
lua
Domaine
devtools
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.