#Can I use a tagged union of error sets to issue different types of errors?

1 messages · Page 1 of 1 (latest)

dapper anvil
#
const error_map = std.ComptimeStringMap(Errors, .{
        .{ "could not locate the selected window.", WindowError.LocateSelected },
        .{ "could not locate a northward managed window.", WindowError.LocateNorth },
        .{ "could not locate a southward managed window.", WindowError.LocateSouth },
        .{ "could not locate a eastward managed window.", WindowError.LocateEast },
        .{ "could not locate a westward managed window.", WindowError.LocateWest },
        .{ "could not locate the first managed window.", WindowError.LocateFirst },
        .{ "could not locate the last managed window.", WindowError.LocateLast },
        .{ "could not locate the next managed window.", WindowError.LocateNext },
        .{ "could not locate the prev managed window.", WindowError.LocatePrev },
        .{ "could not locate the next stacked window.", WindowError.LocateNextStacked },
        .{ "could not locate the prev stacked window.", WindowError.LocatePrevStacked },
        .{ "could not locate the first stacked window.", WindowError.LocateFirstStacked },
        .{ "could not locate the last stacked window.", WindowError.LocateLastStacked },
        // display errors would go here I guess
});

const Errors = union(enum) {
    DisplayError: DisplayError,
    WindowError: WindowError,
};

const WindowError = error{
    LocateSelected,
    LocateNorth,
    LocateSouth,
    LocateEast,
    LocateWest,
    LocateFirst,
    LocateLast,
    LocateNext,
    LocatePrev,
    LocateNextStacked,
    LocatePrevStacked,
    LocateFirstStacked,
    LocateLastStacked,
};

const DisplayError = error{
    LocateNorth,
    LocateSouth,
    LocateEast,
    LocateWest,
};
tall dome
#

the way you must initialise a union is .{ .tag = value }

#

so for the window errors, it must be initialised as e.g. .{ "could not locate the selected window.", .{ .WindowError = WindowError.LocateSelected } }

dapper anvil
#

I see,

#

is this even a smart way to do it

#

I guess I can check multiple string maps

cursive charm
#

You could but I don't see the use case, you could just create an error set that is the union of both sets, e.g.

const Errors = WindowError || DisplayError;

That gives an error set with all errors from WindowError and DisplayError

dapper anvil
#
const Errors = WindowError || DisplayError;

I haven't seen this syntax before...

cursive charm
dapper anvil
#

nice! how do you bubble up a member of one vs the other

tall dome
#

you just return it

#

it coerces regardless of origin

dapper anvil
#

does that mean WindowError.LocateEast and DisplayError.LocateEast will both bubble up as error.LocateEast?

cursive charm
#

It would be Errors.LocateEast, but yeah

#

you wouldn't be able to distinguish them

#

Errors are identified solely by their name, not which error set they belong to, so
WindowError.LocateEast == DisplayError.LocateEast == Errors.LocateEast == error.LocateEast

dapper anvil
#

so when you switch (err) at the callsite you can only do .Locate... correct?

#

not DisplayError.something vs WindowError.something

cursive charm
#

yeah, errors with the same name will be indistinguishable

dapper anvil
#

got it

#

thanks yall

marble kelp
dapper anvil
tall dome
#

it's like defining a subset of the entirety of all errors that a function can return

#

there is a global error set called anyerror

#

an error set is a subset of the global set

#

that's useful for restricting the number of errors that a function is defined to return

wide hornet
dapper anvil
tall dome