@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.

API Reference

Types

StreamThatCanClose

Implements: types.StreamThatCanClose

StringStreamThatCanRead

Implements: types.StringStreamThatCanRead

BufferStreamThatCanRead

Implements: types.BufferStreamThatCanRead

StreamThatCanRead

Implements: types.StreamThatCanRead

StringStreamThatCanWrite

Implements: types.StringStreamThatCanWrite

BufferStreamThatCanWrite

Implements: types.BufferStreamThatCanWrite

StreamThatCanWrite

Implements: types.StreamThatCanWrite

StreamThatCanSeek

Implements: types.StreamThatCanSeek

StreamThatCanFlush

Implements: types.StreamThatCanFlush

ReadOnlyStream

Implements: types.ReadOnlyStream

WriteOnlyStream

Implements: types.WriteOnlyStream

ReadWriteStream

Implements: types.ReadWriteStream

Stream

type Stream = Stream.Stream
Implements: Stream.Stream