News for hackers. Stuff that matters.
Re:Tagged unions, or ‘variants’, in C by k__ on Monday June 26, 2017 @06:19AM (#14635578)
Is there a reason, other than jargon, why this concept is called different names in different languages?

Do variants work different than tagged unions or type classes?

Some people told me that Haskell is superior in FP because of its type classes. Later I read TypeScript has tagged unions, which looked like type classes to me.

Then I looked into Reason and they wrote about variants and I also was reminded of type classes.

Re:Tagged unions, or ‘variants’, in C by yorwba on Monday June 26, 2017 @06:35AM (#14635655)
Tagged unions are an implementation of variants using a discriminating tag and a union type.

Variants are sometimes called sum types to emphasize the duality with product types (tuples).

Type classes are a completely unrelated concept, as others have already commented. They are more like vtables in C++: providing functions to operate on an otherwise opaque object.

Re:Tagged unions, or ‘variants’, in C by k__ on Monday June 26, 2017 @06:39AM (#14635672)
Ah okay.

I thought they were the Haskell equivalent.

I also don't know what vtables are, haha.

Re:Tagged unions, or ‘variants’, in C by klodolph on Monday June 26, 2017 @06:59AM (#14635754)
I don't want to be that guy but there is pretty solid explanation of vtables if you do a web search.

Some of the language also derives from either more traditional set theory (union, disjoint union, discriminated union) or from category theory (sum, coproduct) even though all of these things might mean the same concept in this particular case. Category theory is comparatively "new" (barely 70 years old) and so older works and languages tend not to use it.

Re:Tagged unions, or ‘variants’, in C by ozzmotik on Monday June 26, 2017 @08:32AM (#14636222)
nothing wrong with being that guy but at the same time there's also nothing wrong with admitting you don't know what something is before you go and look it up. i don't want to be that guy but the poster you responded to didn't ask what they were, only stated that they don't know what they are. im sure if they were curious enough and thought it relevant enough to their knowledge base they would go look it up of their own accord.

also, another point I'd really like to bring up is, while in general im comfortable with doing research and learning something from resources on the web, some people are less confident in their abilities to understand something and would prefer to ask another human being to explain it so they can open up the possibility of discussion if they don't particularly understand the verbiage or concepts contained within the answer; it's much easier to ask a person a question than to ask an article or a tutorial one when said resource may not even be actively maintained or monitored anymore and social interaction may have potentially died down on it.

im not trying to really complain as I do believe everyone should really take the time to do their own research and come to their own conclusions, but having someone they can ask is useful in and of itself in the same way having a professor in college you can ask to clarify is being useful. if you don't have another individual that is knowledgeable about the concepts you're unsure about your perception of to potentially confirm or deny your conclusions, then you're basically operating on unknown unknowns, and as far as I understand, in intellectual practice, it is the desire of the practitioner to eliminate any possible unknown unknowns through whatever means necessary and account for all possibilities and deviations.

but yeah, Google first then ask questions!

Re:Tagged unions, or ‘variants’, in C by k__ on Monday June 26, 2017 @08:53AM (#14636347)
Sure, problem is when you 'think' you know something, but you don't, as I thought type classes are equivalent to variants :D
Re:Tagged unions, or ‘variants’, in C by tome on Monday June 26, 2017 @10:40AM (#14637134)
I don't think the word "sum" for disjoint union comes from category theory. It must be older than that.
Re:Tagged unions, or ‘variants’, in C by coot_ on Monday June 26, 2017 @06:52AM (#14635730)
Variants, sum types and type classes are all different notions. In PureScript variants (https://github.com/natefaubion/purescript-variant) are used to have a homogeneous sum type that mixes different types, sum types is a single type with multiple constructors. Type classes others already explained.
Re:Tagged unions, or ‘variants’, in C by tome on Monday June 26, 2017 @06:22AM (#14635592)
The tagged union/variant idea does not correspond to a Haskell type class. In fact it doesn't really even have a name in Haskell it's just the "sum type" aspect of a "algebraic data types" (which constitutes sums, products and functions, basically).
Re:Tagged unions, or ‘variants’, in C by k__ on Monday June 26, 2017 @06:37AM (#14635664)
I see.

I thought that type classes are sum types also.

Re:Tagged unions, or ‘variants’, in C by tome on Monday June 26, 2017 @06:49AM (#14635709)
You can sort of consider them as open sum types which third parties can add additional summands to but that's probably not a very helpful way of thinking about them.
Re:Tagged unions, or ‘variants’, in C by greydius on Monday June 26, 2017 @06:24AM (#14635602)
Type classes are a completely different concept. Tagged unions are sum types.
Re:Tagged unions, or ‘variants’, in C by k__ on Monday June 26, 2017 @06:40AM (#14635673)
I see.

Care to explain what type classes are then? :)

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.
Re:Tagged unions, or ‘variants’, in C by tome on Monday June 26, 2017 @06:50AM (#14635718)
A "type class" specifies an interface and you can make types into "instances" of a "type class" by saying how it implements that interface.