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,
};
#Can I use a tagged union of error sets to issue different types of errors?
1 messages · Page 1 of 1 (latest)
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 } }
I see,
is this even a smart way to do it
I guess I can check multiple string maps
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
the use case would be being able to bubble up different error types
const Errors = WindowError || DisplayError;
I haven't seen this syntax before...
nice! how do you bubble up a member of one vs the other
does that mean WindowError.LocateEast and DisplayError.LocateEast will both bubble up as error.LocateEast?
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
so when you switch (err) at the callsite you can only do .Locate... correct?
not DisplayError.something vs WindowError.something
yeah, errors with the same name will be indistinguishable
I may be wrong, but I think you have to do error.Foo for errors, because .Foo is specifically an enum literal, and an error isn't considered an enum.
Otherwise yes though; Foo.X == Bar.X == error.X, and error.Foo is the equivalent of .Foo for errors.
so what is the use case for error sets?
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
at the callsite, if you want to switch on the error, this lets you switch exhaustively
I can understand that being the use case for the coercion, not the set
That doesn't make any sense. Defining the error set is what allows the call site to switch exhaustively, because that's what tells it what errors to exhaustively check