> Rust's enums and exhaustive match statement are just amazing
ADTs + pattern matching are the killer feature set that makes the more popular functional languages so damn pleasant to use, and they're starting to spread to more and more languages. I suspect that, 50 years from now, we'll look back and see them as the key paradigm shift of this era.
This. I have no idea why people underestimate the conceptual power of ADTs. Every time I try to explain this concept they just disregard it as just a convenient wrapper over union...and union isn't THAT common right..?
Turns out that having a much better tagged union opens up so many possibilities. Software deals with states and what's better to represent states than an ADT? Last time I was fixing an issue where a mouse position is initialized to (0.0f,0.0f) and causes it to trigger a mouse event on startup. The fix was to simply turn it into an Option<(f64,f64)> which is so much better than initializing them as (-1.0f,-1.0f) because it forces you to check all the places where you do arithmetic operations on them.
> This. I have no idea why people underestimate the conceptual power of ADTs.
Blub. It's one of those conceptual steps which seems to have no benefit until it actually clicks and you realise how much it opens or does. Being a tremendously good educator is necessary to make people take that step without experience.
> Is an Algebraic Data Type (ADT) in rust an enum or tuple?
It's the combination of both.
Tuples are Product Types. If you treat types as sets of values, then the type (A,B) is the cartesian product of types A and B. For all practical purposes, structs are just syntactic sugar for tuples. Enums are Sum Types. The values of Result<T, E> are fundamentally just all the values of T plus the values of E (with a tag to tell them apart). Put the two things together, and enum Coordinates{TwoD(f32, f32), ThreeD(f32, f32, f32)} is a set of values with cardinality #f32^2 + #f32^3. You can build types corresponding to arbitrary polynomials, hence the name.
"Union type" usually just means the same as "sum type", though it can also be used to mean C-style unions specifically.
(There's also functions as exponential types, with A -> B having cardinality #B^#A, but you're neck deep in type wankery when you start talking about those :o)
Almost every language supports product types in some way, either implicitly or explicitly, and nobody really bothers talking about those in isolation. It's when you add sum types to the mix that people start talking about ADTs, and it's when you add pattern matching that the whole thing comes into its own and you can just express functions as "things with this shape become things with that shape".
Enums are Sum Types, which are a type of ADT[1].
Tuples are a Product Types, which are another type of ADT[2];
Sum Types are so called because when you add one to that, you add just one more possibility (It could be A,B now it can be A,B,C). Product Types, on the other hand, give out possibilities based on their Product, so if you add a new property, you multiply it (A,B can both be two things, so you have 4 possibilities, if you add C that can also be two things, then you now have 8 possibilities).
You are. But benefit depends how your language is utilizing it. Do your function type check your struct input at compile time? Do your condition statement compiled error when you miss addressing a complete set of enum?
Most Java libraries want to keep compatibility with Java 8, or at most Java 11; there will be more uptake of these kinds of features once the baseline becomes Java 17, which will probably still take a long time.
True. But there are more features out there (such as typeclasses) which are also extremely important and need to become mainstream. We are really not quite there yet.
What makes functional languages so damn pleasant to use is the fact that closures are cleaned up automatically by the runtime system once you're done with them.
Could you give some detail? Is it true for functional language that has mutable data structure and reference as well? How does it clean up, are you talking about "closure conversion"?
ADTs + pattern matching are the killer feature set that makes the more popular functional languages so damn pleasant to use, and they're starting to spread to more and more languages. I suspect that, 50 years from now, we'll look back and see them as the key paradigm shift of this era.