#interface with variable error set that can be empty is annoying, any way to cope?

1 messages · Page 1 of 1 (latest)

umbral saddle
#
const std = @import("std");

test {
    const bytes = @embedFile("test.txt");
    var stream = std.io.fixedBufferStream(bytes);
    const fbs_reader = stream.reader();

    const file = try std.fs.cwd().openFile("test.txt", .{});
    const file_reader = file.reader();
    _ = file_reader; // autofix

    const reader = fbs_reader;
    // const reader = file_reader;

    _ = reader.readByte() catch |err| switch (err) {
        error.EndOfStream => std.debug.print("boo", .{}),
        else => std.debug.print("ceta", .{}),
    };
}

the error set that reader.readByte() produces is ReadError || error{EndOfStream} where ReadError is passed at comptime from the .reader() method of FixedBufferStream or File

for FBS, ReadError is empty while for File it has a bunch of errors like AccessDenied, BrokenPipe, NotOpenForReading, etc

this means that switching on the error produced by .readByte() is a compile error, in this case, using fbs_reader i get compile error saying that the else prong is useless, but removing it means that it is now a compile error with file_reader because the other possible errors were not handled

what do

low flint
#

probably not a good solution but does switch(@as(anyerror, err)) work

umbral saddle
#

that works

#

but now if i return the error on the else, it means that my function returns anyerror and not a cool error set with all values clearly laid out

low flint
#

does errorSetCast exist?

#

oh its called errorCast else => @as(Reader.Error, @errorCast(err))

umbral saddle
#

i'm getting a weird error now

#

documentation of errorCast says it works for error unions and error sets

#

but the error is
error: expected error union type, found 'error{EndOfStream}'

low flint
#

ohhh so youd have to do @as(Reader.Error, @errorCast(@as(anyerror!void, err))))

#

i think

#

ive never really used this builtin

umbral saddle
#

well now it works

#

so documentation is not matching the implementation?

low flint
#

i dont really know how you would cast an error set, but err would be an error value, not a set

umbral saddle
#

also look at this other weird error if i omit anyerror :P ts const byte = reader.readByte() catch |err| return switch (@as(anyerror, err)) { error.EndOfStream => error.UnclosedQuote, else => @errorCast(@as(!void, err)), }; ```
error: expected type 'bool', found 'type'
else => @errorCast(@as(!void, err)),

low flint
#

it thinks you wanna use the ! bool operator on void :P

umbral saddle
#

ah

#

i think !void is not valid outside of function return type

#

thanks

low flint
#

np, might be a more elegant way, considering it says something about error sets

rigid monolith