Edit: passing the function and the arguments to the function as separate arguments is the key to why this does work. I missed that earlier.
Original reply:
That won’t work. The assumption is that the add function here is the expensive part, not the assert; exiting the assert early does not matter. Try adding a print to your add function and observe that it is always called.
fn add(a: u64, b: u64) u64 {
std.debug.print("in add\n", .{});
return a + b;
}
(Technically the print statement forces add to always be called because it introduces a side effect, and the non side-effecting function could have been optimized away, but I think you should write your code in a way that it doesn’t matter whether or not the function has side effects if it is important that it is not included in certain build modes)
This is because there are no macros in Zig, as the OP said. The std.debug.assert function call being optimized away does not mean that functions called as arguments are optimized away.
This comment and the replies have some relevant discussion that may be interesting