mirror of
https://github.com/ziglang/zig.git
synced 2025-12-15 02:33:07 +00:00
28 lines
607 B
Zig
28 lines
607 B
Zig
// With an inferred error set
|
|
pub fn add_inferred(comptime T: type, a: T, b: T) !T {
|
|
const ov = @addWithOverflow(a, b);
|
|
if (ov[1] != 0) return error.Overflow;
|
|
return ov[0];
|
|
}
|
|
|
|
// With an explicit error set
|
|
pub fn add_explicit(comptime T: type, a: T, b: T) Error!T {
|
|
const ov = @addWithOverflow(a, b);
|
|
if (ov[1] != 0) return error.Overflow;
|
|
return ov[0];
|
|
}
|
|
|
|
const Error = error {
|
|
Overflow,
|
|
};
|
|
|
|
const std = @import("std");
|
|
|
|
test "inferred error set" {
|
|
if (add_inferred(u8, 255, 1)) |_| unreachable else |err| switch (err) {
|
|
error.Overflow => {}, // ok
|
|
}
|
|
}
|
|
|
|
// test
|