Hacker News new | ask | show | jobs
by cgh 503 days ago
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(),
          }
      }
  };
1 comments

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