@eryx/stream Module
Functions that return a stream will return a ReadOnlyStream, a WriteOnlyStream, or a ReadWriteStream.
Stream represent a generic stream of unknown writability, but
readable and writable allow this to be narrowed down. For
example:
if s.readable then
s:read()
end
if s.writable then
s:write()
end
When writing a function that expects a generic stream as an
argument, the StreamThatCanX types should be used instead. If a
function is defined as taking a ReadOnlyStream, the type system
will not allow a ReadWriteStream to be passed. On the other
hand, that same function can be defined to take a
StreamThatCanRead & StreamThatCanClose and a read-write stream can
be passed.
local function foo(x: ReadOnlyStream)
print(x:read())
x:close()
end
foo(y :: ReadWriteStream) -- Type error
local function foo(x: StreamThatCanRead & StreamThatCanClose)
print(x:read())
x:close()
end
foo(y :: ReadWriteStream) -- Ok!
This way, a function's argument describes the expected capabilities required of the passed stream.