I am reading docs https://docs.harelang.org/bufio#read_line
Signature of function is next:
fn read_line(file: io::handle) ([]u8 | io::EOF | io::error);
But follow code is reqired using `as` keyword to get it's compiled:
use fmt;
use io;
use bufio;
use memio;
use strings;
export fn main() void = {
let mystr = strings::toutf8("hello\nworld");
let buf = memio::fixed(mystr);
const line = bufio::read_line(&buf) as []u8;
fmt::println(len(line))!;
};
It seems that it is forced to return the first tagged union, but I am
not sure about it.
Also how can I handle errors with `match` and `[]u8`?
"as" is a "type assertion", it asserts that a tagged union has a
particular type and pulls out the value of that type. If the value has a
different type, a runtime assertion is raised. In this sample code, we
can know in advance that it will not fail because we construct a memio
reader which always has data available for the first read.
You can handle errors like so:
const line = match (bufio::read_line(&buf)) {
case let line: []u8 =>
yield line;
case io::EOF =>
// handle EOF
case let err: io::error =>
// do something with err
};
// do something with line