I'm not usually in the "defending C++" camp, but when I see this:
pub const File = struct {
pub fn writer(self: *File, buffer: []u8) Writer{
return .{
.file = self,
.interface = std.Io.Writer{
.buffer = buffer,
.vtable = .{.drain = Writer.drain},
}
};
}
pub const Writer = struct {
file: *File,
interface: std.Io.Writer,
// this has a bunch of other fields
fn drain(io_w: *Writer, data: []const []const u8, splat: usize) !usize {
const self: *Writer = @fieldParentPtr("interface", io_w);
// ....
}
}
}
...I can't help but think of this: struct FileWriter: public Writer {
File *file;
// this has a bunch of other fields
FileWriter(File *self, span<char> buffer)
: Writer(buffer), file(self) {}
size_t drain(span<span<char const> const> data, size_t splat) override {
// ....
}
};
Writing code to build a vtable and having it implicitly run at compile time is pretty neat, though!