LuaLS / LuaLS/lua-language-server
[Feature Request] Interfaces and Implementors
還沒有人認領這個 Issue。
- 主要語言
- Lua
- 星號
- 4.4k
- 分支
- 442
- PR 合併指標
- 30 天內沒有已合併 PR
描述
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`).
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 issue 中描述的類別繼承和型別註解行為開始,包括 Stream、ReadStream、SeekableStream 和 StringStream 的範例。比較所提出的合取型別和介面方法,然後驗證當實作省略自身的註解時,繼承的方法註解仍會驗證引數和回傳型別。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- lua
- 領域
- developer-experience, devtools
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100