You're checking the out_error for nullity every time you call the function.
Having said that, the optional parameter works nicely too, and (this is a matter of taste, I suppose) easier to read in the case you don't have other parameter options.
For further actions, you may consider blocking this person and/or reporting abuse
I don't see what the compile time reflection gets you here. The same thing can be accomplished more simply with an optional pointer.
const std = @import("std");
const CustomError = error{NumberError};
const NumberErrorPayload = struct {
message: []const u8 = undefined,
value: u64 = undefined,
};
pub fn raise(message: []const u8, value: u64, out_error: ?*NumberErrorPayload) CustomError {
if (out_error) |opts|{
opts.message = message;
opts.value = value;
}
return CustomError.NumberError;
}
fn add_one(number: u64, out_error: ?*NumberErrorPayload) CustomError!u64 {
if (number == 42) {
return raise("bad number encountered", number, out_error);
} else {
return number + 1;
}
}
pub fn main() !void {
std.debug.print("no error: {}\n", .{try add_one(1, null)});
}
You're checking the out_error for nullity every time you call the function.
Having said that, the optional parameter works nicely too, and (this is a matter of taste, I suppose) easier to read in the case you don't have other parameter options.