LuaLS / LuaLS/lua-language-server
[Feature Request] Interfaces and Implementors
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
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`).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the class inheritance and type-annotation behavior described in the issue, including the Stream, ReadStream, SeekableStream, and StringStream examples. Compare the proposed conjunction type and interface approaches, then verify that inherited method annotations still validate arguments and return types when implementations omit their own annotations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- developer-experience, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100