Given,
test {
defer _ = foo;
const foo = 6;
}
What happens we run zig test
upon it?
- Test passes.
- Test fails with panic.
- Compile error: use of undeclared identifier 'foo'.
- Compile error: unused local constant.
- None of the above.
The answer is after the giraffe.
The answer is C. Compile error: use of undeclared identifier 'foo'.
This makes sense if you consider a block can return before an identifier is declared. This would cause the defer to refer to an non-existent identifier.
test {
defer std.debug.print("{}", .{ foo });
if (maybe) {
// if original test did pass, the defer would be required to print foo here
return;
}
const foo = 6;
}
Top comments (0)