logoalt Hacker News

sphlast Sunday at 12:54 PM1 replyview on HN

Tagged enums != any type (i.e. runtime casting)

Tagged enums are everywhere. I am writing a micro kernel in C and how I wish I had tagged enums instead of writing the same boilerplate of

  enum foo_type {
    FOO_POINTER,
    FOO_INT,
    FOO_FLOAT,
  };

  struct foo {
    enum foo_type type;
    union {
      void *val_pointer;
      int val_int;
      float val_float;
    };
  };

Replies

flohofwoelast Sunday at 1:51 PM

> ...runtime casting...

...what else is a select on a tagged union than 'runtime casting' though. You have a single 'sum type' which you don't know what concrete type it actually is at runtime until you look at the tag and 'cast' to the concrete type associated with the tag. The fact that some languages have syntax sugar for the selection doesn't make the runtime overhead magically disappear.

show 1 reply