Remix.run Logo
▲ quietbritishjim 3 hours ago

That doesn't work if you are writing a library where users of the library are able to implement their own event types.

Even for a self-contained code base, it's a pain compared to a type that just works with any current or future derived class.

▲nananana9 3 hours ago | parent | next [-]

It does.

  enum EventType {
     ...,
     EVENT_USER_START = 0xF0000000,
  };
  struct UserEventData { uint8 placeholder[64]; }
  struct Event {
     EventType type;
     union {
         UserEventData user;
     };
  }
That aside, have you ever felt the need to implement your own Win32 event, or SDL event, or X11 event, or epoll_event, or a custom DOM event or whatever?

If you're writing a good library, give the user the data they need, and let them handle it however they wish, don't force them int your framework.

▲quietbritishjim an hour ago | parent | next [-]

That doesn't work if your library needs to handle these events somehow, not just transparently pass them around.

Typically with your pattern, you'd use a switch statement on the event type, but what do you do if it's in the user-defined space (type>=EVENT_USER_START)? You could add mandatory function pointers for relevant handlers in a header prefix, but now you're just reimplementing virtual methods. As is common in C, you end up making your own object-oriented language, only with more effort and not as good.

▲marckerbiquet 2 hours ago | parent | prev [-]

It does not work if two independent libraries depend on your library and create the same user EventType.

▲gpderetta 2 hours ago | parent | prev [-]

https://en.wikipedia.org/wiki/Expression_problem