LuaLS / LuaLS/lua-language-server
[Feature Request] Interfaces and Implementors
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
The current class system is alright, but imagine this: I have a `Stream` class which is extended by `ReadStream`, `WriteStream`, `SeekableStream`, etc.
```lua
---@class Stream
---@field close fun() Deallocates or hangs up a stream.
---@class SeekableStream: Stream
---@field seek fun(whence: SeekWhence, offset: integer): integer
---@class ReadStream: Stream
---@field read fun(self: ReadStream, length: integer): string|nil
---@class WriteStream: Stream
---@field write fun(data: string): integer
---@class StringStream: ReadStream, SeekableStream Streams data out of a string.
```
Now I have a function which takes any Seekable, Readable string. How do I specify this? Either we need a conjunction type (`ReadStream & SeekableStream`, which has keys from both), or we need (preferably) interfaces, which are generic and can be extended by classes. The reason I suggest interfaces is because if I write this:
```lua
---@param length integer
---@return string|nil
function StringStream:read(length)
if self.buffer == nil then
error("Stream is closed", 2)
end
if self.cursor > #self.buffer then
return nil
end
local out = string.sub(self.buffer, self.cursor, math.min(#self.buffer, self.cursor + length))
self.cursor = self.cursor + #out
return out
end
```
and I omit the type annotation, it assumes `read` is already defined because `StringStream` extends `ReadStream`, and it doesn't validate types (arguments and return type are `any`).
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
Inizia con il comportamento dell’ereditarietà delle classi e delle annotazioni di tipo descritto nell’issue, inclusi gli esempi di Stream, ReadStream, SeekableStream e StringStream. Confronta gli approcci proposti del tipo congiunzione e delle interfacce, quindi verifica che le annotazioni dei metodi ereditati continuino a validare gli argomenti e i tipi restituiti quando le implementazioni omettono le proprie annotazioni.
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
- 25/100