LuaLS / LuaLS/lua-language-server
Best way to implement something like an abstract base class?
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
I'm working on a framework where users of it implement a kind of module that mixes their provided logic with some optional event handlers.
Here is a simple example
-- provided by framework
---@class Base
---@field OnClick fun(self: Base, x: number, y: number)
local base = {}
---@generic T: Base
---@param name `T`
---@return T
function New(name)
return setmetatable({}, {__index = base})
end
-- usage of framework by user
---@class MyImpl: Base
local foo = New("MyImpl")
function foo:OnClick(x, y) -- x and y are inferred as any instead of number :(
end
function foo:myHelperFunction()
end
So I expect the user to implement OnClick and fill it with their logic. My goal here is that the user automatically gets the correct types on the parameters for foo:OnClick. Here in this simple example it's just two numbers, but my real app has like > 10 such methods and some of these take complex types as parameters.
With the code above it doesn't know anything about the parameters in OnClick and shows them as any. Maybe I got something wrong with the generics, but even without all the fancy generic stuff (just @return base on New) it's the same problem.
The only work-around I found was to not use @class on the user-defined class and not define a generic, this makes the parameters on the OnCLick function definition correct! However, it now triggers injected-field on all user-defined values not defined in the base class like the myHelperFunction method :(
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riprodurre l'esempio Lua annotato dell'issue #2453 e verificare come il language server gestisce i ritorni generici, i metodi ereditati e i campi aggiunti dall'utente. Il lavoro è completato quando i parametri OnClick sovrascritti vengono inferiti come number, mentre i metodi definiti dall'utente, come myHelperFunction, non producono diagnostica relativa ai campi iniettati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- lua
- Ambito
- developer-experience, devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100