Hello all! I'm brand new here and to Zig.
I've built 0.14.0 from source for fun (since it sounds like a release is coming soon) and I've been playing around with things to get a feel for the language.
Right now I'm exploring error handling. Would this be considered idiomatic zig for dealing with custom error types or is there a more concise way to do this?
const std = @import("std");
pub const Error = error{
InvalidValue,
};
const Person = struct {
name: *const [50]u8,
age: u8,
pub fn init(n: *const [50]u8, a: u8) !Person {
if (a > 100) return error.InvalidValue;
return Person{ .name = n, .age = a };
}
pub fn do_stuff(p: Person) void {
std.debug.print("Name: {s}\n", .{p.name});
std.debug.print("Age: {}\n", .{p.age});
}
};
pub fn main() void {
if (Person.init("John Dork", 101)) |p| {
p.do_stuff();
} else |err| switch (err) {
error.InvalidValue => std.debug.print("You died, your age needs to be > 100!", .{}),
}
}