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

I don’t think it is appropriate to say Rust has ‘union types’. Rust has sum types, implemented as Enums and (unsafe) Union types. There is a distinct difference between sum types and union types from a type theoretic perspective.


disjoint union vs union.

Scala3 is the only programming language to implement both AFAIK.

C# has a proposal to add both unions and disjoint unions: https://github.com/dotnet/csharplang/blob/main/proposals/Typ...

OCaml has polymorphic variants which are open disjoint unions.

Kotlin is looking to add union types for errors: https://youtrack.jetbrains.com/issue/KT-68296/Union-Types-fo...

I believe Java's checked exceptions behave somewhat like union types.


What’s the difference between a union type and a disjoint union type? In that C# proposal I couldn’t tell which syntax was which branch of your dichotomy.


disjoint union is sum type / enum / algebraic data type. Defined at the point of declaration. Each case is distinct (hence, disjoint)

union is what Typescript has. Defined at the point of use. Cases need not be distinct.


> Rust has sum types, implemented as Enums

Do you mean implemented with enums? Enums themselves are not a type. They are a mechanism for value generation, providing automatic numbering (hence enumeration) for constants. Indeed, they, like all values, are ultimately represented by a type, but that type can range from something like a simple integer or something more complex like a tagged union (typically with the generated value being the tag) with different ecosystems favouring different type approaches.


I think they just mean that sum types are defined by the programmer using the `enum` keyword


Like how subroutines are implemented as Functions.

And by Functions you don't mean functions, but rather the letters fn?

That is certainly an interesting way to communicate.


Off topic, but rust really messed up the terminology by using 'enums' for sum types.




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

Search: