News for hackers. Stuff that matters.
Re:Tagged unions, or ‘variants’, in C by comex on Monday June 26, 2017 @06:55AM (#14635740)
They’re known in other languages as interfaces, traits, or protocols: just a set of methods which can be implemented for different types.

Importantly, it’s possible to implement type classes for existing types in a separate declaration, as opposed to traditional OO languages where a list of implemented interfaces has to be declared upfront as part of the class declaration. Thus, for instance, a client of a library can declare its own type class and implement it for the library’s types. (Many languages other than Haskell support this too, though.)

Re:Tagged unions, or ‘variants’, in C by k__ on Monday June 26, 2017 @07:04AM (#14635787)
So they're a bit like open classes in Ruby or implicit conversions in Scala?
Re:Tagged unions, or ‘variants’, in C by jnbiche on Monday June 26, 2017 @08:31AM (#14636217)
If you're familiar with Scala, then type classes in Haskell are pretty close to traits in Scala (but not implicit conversions, which can make use of traits, but are an orthogonal language feature). They're also close to Rust's traits, it you're familiar with Rust.
Re:Tagged unions, or ‘variants’, in C by k__ on Monday June 26, 2017 @08:54AM (#14636358)
I see. Thanks.
Re:Tagged unions, or ‘variants’, in C by lmm on Monday June 26, 2017 @07:32AM (#14635909)
They achieve the same result, but in a quite different way. I see them as decoupling the "implements X" piece of a traditional OO class in a language that has interfaces from the class itself.