cerror-handlingabizig

how handle Zig error sets from C using Zig ABI


I want to call a zig function from c but the fn return type is an error union !void or MyType.Error!u32

how to handle the error?

let's assume we have the following lib.zig

pub fn errorProneFunction(param1: *Task, param2: u32) Error!u32 {
    return Error.TooManyTasksForLazyMe;
}

I want to call the function from c

type??? my_var = errorProneFunction(&task, 0);

Solution

  • If you want to call a Zig function from C, then you need to declare that function as export. And such functions cannot return an error union. The compiler will give an error like that:

    src\main.zig:3157:33: error: return type '@typeInfo(@typeInfo(@TypeOf(main.errorProneFunction)).Fn.return_type.?).ErrorUnion.error_set!u32' not allowed in function with calling convention 'C'
    export fn errorProneFunction() !u32 {
                                    ^~~