Comment by throwawaymaths

Comment by throwawaymaths 11 hours ago

0 replies

here is example code. you wont "use the wrong io".

    const VTable = struct {
      f: &fn (*VTable) void,
    };

    const A = struct {
      io: IO,
      v: VTable = .{ .f = &A.uses_io },
      fn uses_io(this: *VTable) void {
        const self: *A = @fieldParentPtr(.v, this);
        self.io.some_io_fn(...);
      }
    };

    const B = struct{v: VTable = .{.f = &void_fn}};
    fn void_fn(_: *VTable) void {}

    pub fn calls_vtable(v: VTable) {
      v.f()
    }