Now that Zig’s default magic format function has become more convenient in 0.15.1, I dediced to implement some in my program.
This writes some array to the writer (using the Move`s format as well):
pub fn format(self: Node, writer: *std.io.Writer) std.io.Writer.Error!void {
const len: usize = self.pv.len;
if (len == 0) return;
for (self.pv.buffer[0..len - 1]) |move| {
try writer.print("{f} ", .{ move });
}
const last: Move = self.pv.buffer[len - 1];
try writer.print("{f}", .{ last });
Notice how I have to split the code to prevent a trailing space.
In C# I was used to (for example) the easy string.join(separator, array)
function.
Would there be an easier way to do this above function?
Another question:
I can imagine structs can have multiple formats. Is there a way to easily implement “custom formatters”?