I don't understand this compiler error

Here is some code:

pub fn convertAnimationSet(spec: specs.AnimationSetSpec, sheet: core.graphics.Sheet, allocator: std.mem.Allocator) 
    struct { skeleton: core.animation.Skeleton, skin: core.animation.Skin } {

    // ... stuff here, create "result" and "skin" variables for later

    // the next line is where I get an error
    const animBuilder = try core.animation.AnimationBuilder.init(std.heap.page_allocator, 10);
    _ = animBuilder;

    return .{ .skeleton = result, .skin = skin };
}

Here’s the error I’m getting:

src/game/animation.zig:117:25: error: expected type 'animation.convertAnimationSet__struct_34611', found '@typeInfo(@typeInfo(@TypeOf(animation.animationBuilder.AnimationBuilder.init)).Fn.return_type.?).ErrorUnion.error_set'
    const animBuilder = try core.animation.AnimationBuilder.init(std.heap.page_allocator, 10);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/game/animation.zig:7:5: note: struct declared here
    struct { skeleton: core.animation.Skeleton, skin: core.animation.Skin } {
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/game/animation.zig:7:5: note: function cannot return an error
    struct { skeleton: core.animation.Skeleton, skin: core.animation.Skin } {

I really don’t understand why it’s complaining about the call to core.animation.AnimationBuilder.init(). It seems like the compiler thinks I’m trying to return the result of core.animation.AnimationBuilder.init() directly from this function, which is wrong.

Am I doing something wrong, or is this a compiler bug? I’m using Zig 0.12.0-dev.2063+804cee3b9.

You need to specify that your function can return an error. You need a ! on the return type.

You’re right. I forgot that try adds an implicit return. Time to go to bed.

1 Like

Of course, in time you learn to quickly spot these errors for what they are (unless you are too sleepy :slight_smile: ), but they could certainly be clearer about the actual problem and (typical) solution.