You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current class system is alright, but imagine this: I have a Stream class which is extended by ReadStream, WriteStream, SeekableStream, etc.
---@classStream---@fieldclosefun() Deallocates or hangs up a stream.---@classSeekableStream:Stream---@fieldseekfun(whence: SeekWhence, offset: integer): integer---@classReadStream:Stream---@fieldreadfun(self: ReadStream, length: integer): string|nil---@classWriteStream:Stream---@fieldwritefun(data: string): integer---@classStringStream:ReadStream,SeekableStreamStreamsdataoutofastring.
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:
---@paramlengthinteger---@returnstring|nilfunctionStringStream:read(length)
ifself.buffer==nilthenerror("Stream is closed", 2)
endifself.cursor>#self.bufferthenreturnnilendlocalout=string.sub(self.buffer, self.cursor, math.min(#self.buffer, self.cursor+length))
self.cursor=self.cursor+#outreturnoutend
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).
The text was updated successfully, but these errors were encountered:
From your reproduction code snippet, I believe there maybe some bugs when defining the interface using @field syntax 😕
Because when you use @field to define the read() method, no type inference on the implemented method param.
---@classReadStream---@fieldreadfun(self: ReadStream, length: integer): string|nillocalReadStream---@classStringStream:ReadStream---@classStringStreamlocalStringStreamfunctionStringStream:read(length) --> length: anyendStringStream:read("") --> no type checking
current workaround
So the current workaround is to define the interface using function style (maybe you already have a separate @meta file?), instead of @field, before this issue is fixed.
The current class system is alright, but imagine this: I have a
Stream
class which is extended byReadStream
,WriteStream
,SeekableStream
, etc.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:and I omit the type annotation, it assumes
read
is already defined becauseStringStream
extendsReadStream
, and it doesn't validate types (arguments and return type areany
).The text was updated successfully, but these errors were encountered: