Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I mean union types in the programming language theory sense, not in the "we called this language feature union types" sense. Elm's union types are algebraic data types (ADTs). Union types in the PLT sense are what Typescript has. The main difference is that an ADT must be declared upfront, whilst a union type can be constructed in an ad-hoc manner based on whatever types are used together at a particular point in the program.




I used to think the same but after having many discussions just like this I accepted that there’s no agreed upon formal definition for what is a union type and what isn’t. Being created ad hoc or not seems like a made up on the spot distinction. If you do think there is a definition that most researchers agree on that’s distinct from ADT, do provide some sources.

Here's Types and Programming Languages on union types. My take away is that the preferred terminology is union types for non-disjoint unions and sum types for disjoint unions.

"Sum and variant types are sometimes called disjoint unions. The type T1+T2 is a “union” of T1 and T2 in the sense that its elements include all the elements from T1 and T2. This union is disjoint because the sets of elements of T1 or T2 are tagged with inl or inr,respectively, before they are combined, so that it is always clear whether a given element of the union comes from T1 or T2. The phrase union type is also used to refer to untagged (non-disjoint) union types, described in §15.7." p142

"The dual notion of union types, T1 ∨ T2, also turns out to be quite useful. Unlike sum and variant types (which, confusingly, are sometimes also called “unions”), T1 ∨T2 denotes the ordinary union of the set of values belonging to T1 and the set of values belonging to T2, with no added tag to identify the origin of a given element." p207


They seem to both agree that union types are about being possibly non-disjoint, and ADT being always disjoint, with values tagged as of a single type. I can accept that since it defines union as the same concept as in set theory. There’s nothing there regarding types being ad hoc, though that is arguably a consequence of the syntax since using a symbol between different types to “union” them cannot be expected to result in a disjointed type union. So, I think that’s a useful and defensible position, but I don’t think many people will use these definitions consistently given there’s already a lot of confusion in the use of these terms.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: