mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
e3424332d3
* `doc/langref` formatting * upgrade `.{ .path = "..." }` to `b.path("...")` * avoid using arguments named `self` * make `Build.Step.Id` usage more consistent * add `Build.pathResolve` * use `pathJoin` and `pathResolve` everywhere * make sure `Build.LazyPath.getPath2` returns an absolute path
28 lines
606 B
Zig
28 lines
606 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
|