master
1// With an inferred error set
2pub fn add_inferred(comptime T: type, a: T, b: T) !T {
3 const ov = @addWithOverflow(a, b);
4 if (ov[1] != 0) return error.Overflow;
5 return ov[0];
6}
7
8// With an explicit error set
9pub fn add_explicit(comptime T: type, a: T, b: T) Error!T {
10 const ov = @addWithOverflow(a, b);
11 if (ov[1] != 0) return error.Overflow;
12 return ov[0];
13}
14
15const Error = error{
16 Overflow,
17};
18
19const std = @import("std");
20
21test "inferred error set" {
22 if (add_inferred(u8, 255, 1)) |_| unreachable else |err| switch (err) {
23 error.Overflow => {}, // ok
24 }
25}
26
27// test