Discussion on: Howto Pair Strings with Enums

lhp profile image
Leon Henrik Plickat

Why not just switch on the error and avoid the enum?

fn errorMessage(error: anyerror) []const u8 {
    switch(error) {
        error.MyFancyError => return "My fancy error happened",
        error.DeviceIsLitrallyOnFire => return "Check lp0",
        else => return "unexpected error",
Enter fullscreen mode Exit fullscreen mode

Although I personally think it's better to just switch locally at the callsite of the function that may fail, like so:

mightFail() catch |err| {
    switch(err) {
        error.Whoops => log.err("something went wrong", .{}),
        else => log.err("unexpected error: {}", .{err}),
Enter fullscreen mode Exit fullscreen mode
jpl profile image

I am progressing slowly in my study of the "Zig-lang" language, today I see why to implement the last solution.
Ps: nothing better than transcribing a software from one language to another, because we don't care about the conceptual, but about the language.

david_vanderson profile image
David Vanderson

Yes I would normally do that too. The question asked about having an enum that was being displayed in a gui, and switching on the selected value. Lots of great ways to do it!