Zig NEWS

Discussion on: Howto Pair Strings with Enums

Collapse
 
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
Collapse
 
jpl profile image
Jean-Pierre

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.

Collapse
 
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!