News for hackers. Stuff that matters.
Re:In Zig, What's a Writer? by cgh on Saturday February 01, 2025 @04:28PM (#42902547)
You can just used tagged enums and the inline else syntax, like this:

  const Animal = union(enum) {
      cat: Cat,
      dog: Dog,

      pub fn make_noise(self: Animal) void {
          switch (self) {
              inline else => |impl| impl.make_noise(),
          }
      }
  };
Re:In Zig, What's a Writer? by 2c2c2c on Saturday February 01, 2025 @08:05PM (#42904452)
iirc there's multiple idioms that are used in different cases. i recall a nice github that laid them all out with use cases but I can't find it