#How to return from a block?

1 messages · Page 1 of 1 (latest)

tribal topaz
#
const CustomError = error{
    SomeError,
};

pub fn foo() !bool {
    return CustomError.SomeError;
}

pub fn main() !void {
    const val: bool = foo() catch |e| {
        std.log.debug("{}", .{e});
        return false;
    };

    std.log.debug("Final: {}", .{val});
}

This code fails to compile. The return statement tries to return from the main.

rustic thistle
#

you use

const value: bool = block_name: {
  break :block_name false;
};

so you need a block name for your catch block and then use break

tribal topaz
#

Something like get the value, if there's an error replace it with this value instead pattern?

rustic thistle
#

thats what catch does

#

ie int_or_error() catch 10;

tribal topaz
#

const val: bool = foo() catch false;

#

Thanks

rustic thistle
#

np zeroLike