mirror of
https://github.com/ziglang/zig.git
synced 2024-12-01 17:42:31 +00:00
36 lines
816 B
Zig
36 lines
816 B
Zig
|
var result: [3]u8 = undefined;
|
||
|
var index: usize = undefined;
|
||
|
|
||
|
error FalseNotAllowed;
|
||
|
|
||
|
fn runSomeDefers(x: bool) -> %bool {
|
||
|
index = 0;
|
||
|
defer {result[index] = 'a'; index += 1;};
|
||
|
%defer {result[index] = 'b'; index += 1;};
|
||
|
defer {result[index] = 'c'; index += 1;};
|
||
|
return if (x) x else error.FalseNotAllowed;
|
||
|
}
|
||
|
|
||
|
fn mixingNormalAndErrorDefers() {
|
||
|
@setFnTest(this);
|
||
|
|
||
|
assert(%%runSomeDefers(true));
|
||
|
assert(result[0] == 'c');
|
||
|
assert(result[1] == 'a');
|
||
|
|
||
|
const ok = runSomeDefers(false) %% |err| {
|
||
|
assert(err == error.FalseNotAllowed);
|
||
|
true
|
||
|
};
|
||
|
assert(ok);
|
||
|
assert(result[0] == 'c');
|
||
|
assert(result[1] == 'b');
|
||
|
assert(result[2] == 'a');
|
||
|
}
|
||
|
|
||
|
// TODO const assert = @import("std").debug.assert;
|
||
|
fn assert(ok: bool) {
|
||
|
if (!ok)
|
||
|
@unreachable();
|
||
|
}
|