#How on earth do you understand Zig error messages?

1 messages · Page 1 of 1 (latest)

outer locust
#

Here is the original code:

const std = @import("std");

fn retEight() !u8 {
    return 8;
}

pub fn main() !void {
    if (retEight()) |val| {
        std.debug.print("{}", .{val});
    } else {
        std.debug.print("{}", .{42});
    }
}

Here is what I get:

zig run main.zig
main.zig:8:17: error: expected optional type, found '@typeInfo(@typeInfo(@TypeOf(main.retEight)).Fn.return_type.?).ErrorUnion.error_set!u8'
    if (retEight()) |val| {
        ~~~~~~~~^~
referenced by:
    callMain: /Users/timfayz/zig/lib/std/start.zig:608:32
    initEventLoopAndCallMain: /Users/timfayz/zig/lib/std/start.zig:542:34
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

What I was actually supposed to do is to add |_| in the else to make things going. Or formally, since I used the payload capturing in the if part, the control structure is probably expecting the error capturing part. However, the error message says that zig expected optional type but I already provided it exactly in the place it is referring to! retEight() is of type !u8.

Could anyone explain the logic behind this error message and how a developer is supposed to read it in order to fix the error? 🥲

toxic heath
#

theres not really a trick other than being familiar with the language

#

the error is unhelpful but correct, what you did is only valid for optionals, not much you can do until its more mature and has a special case to detect what youre trying to do

outer locust
#

the error is unhelpful but correct
In which sense is it correct?

toxic heath
#

what you did is only correct with optionals, it said it expected an optional

outer locust
#

Sorry, completely lost. What exactly did I do?

toxic heath
#

if on an error union without capturing the else

storm cloak
#

The issue is that both:

if(something) |val| {} else {}

and

if(something) |val| {} else |err| {}

Are correct syntax, but the first syntax expects something to be an optional, while the latter expects an error union.
Problem seems to be that the parser looks at which syntax is used, and only sees that the wrong type was used without considering you might want the other form instead.

outer locust
#

God sake...

#

I got it guys. My fault 🙂

toxic heath
#

just a consequence of being a young language, its easy to be confused by

outer locust
#

I indeed didn't provide an optional type and provided the error union type instead 😬

storm cloak
#

To be fair, the way error messages tell you that it sees an error union is pretty archaic.

'@typeInfo(@typeInfo(@TypeOf(main.retEight)).Fn.return_type.?).ErrorUnion.error_set!u8'

Nearly completely unintelligible.

outer locust
#

I hope it isn't offsite but I always wondered why it uses @TypeInfo twice in there?

storm cloak
#

The inner one is getting the type of the function retEight, and then the outter one gets the type of the field @typeInfo(@TypeOf(main.retEight)).Fn.return_type.? (so its return type)