logoalt Hacker News

p1necone • today at 12:21 AM • 1 reply • view on HN

One simple but non obvious (at least to me) trick when implementing tagged unions: put the union discriminator/tag at the end, not the beginning - that way a pointer to a union is also a pointer to the value, no need to increment the pointer to point past the tag after discriminating.


Replies

beached_whale • today at 1:31 AM

when serializing these things it's generally going to be best to put the tag first though. That way it's cheap to figure out how to parse it.

➕ show 1 reply