Does including an exclamation point for functions returning void mean that the function could return either void or an error? And if so, would running if(result) |val| else |err| Always go to the else statement (assuming zig allows comparing void or casting void to bool) Is this case different or does it work the same as any other type?
I don't think you can capture the variable like this: if (result) |val| You can't use a void type, so capturing won't work. You can do this though: if (result) { std.debug.print("No errors ", .{}); } else |err| { std.debug.print("Error {} ", .{err}); } If you just return from that function call, then 'result' will be void and not an error so will print "No errors". Using a variable in an if like this: if (result) This simply mean if the variable isn't an error. It won't matter the type, void or not.
I like the example
Does including an exclamation point for functions returning void mean that the function could return either void or an error? And if so, would running
if(result) |val|
else |err|
Always go to the else statement (assuming zig allows comparing void or casting void to bool)
Is this case different or does it work the same as any other type?
I don't think you can capture the variable like this:
if (result) |val|
You can't use a void type, so capturing won't work. You can do this though:
if (result) {
std.debug.print("No errors
", .{});
}
else |err| {
std.debug.print("Error {}
", .{err});
}
If you just return from that function call, then 'result' will be void and not an error so will print "No errors".
Using a variable in an if like this:
if (result)
This simply mean if the variable isn't an error. It won't matter the type, void or not.