7 ms·
> Rust's enums and exhaustive match statement are just amazing ADTs + pattern matching are the killer feature set that makes the more popular functional langua
by pdpi 4y ago
> 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.
- jmfldn 4y agoAs a Scala dev I agree.
- qaq 4y agoADTs is last missing piece that would make me fully content with Go :)
- kajaktum 4y agoI agree, I love that Go is simple but it is simply too...simple. It makes it hard to represent a much harder state transformations.
- deleted 4y ago[deleted]
- achenet 4y agoaren't interfaces technically an abstract data type? EDIT: ah, it's algebraic data types, my bad, apologies.
- xigoi 4y agoInterfaces are not closed, which is considered a big advantage of sum types (though it can also be a disadvantage).
- pjmlp 4y agoPity that it took people 50 years to discover them.
- pooya72 4y agoYeah, it's interesting to see them become a part of Java with records, sealed types, and pattern matching. However, I haven't seen much uptake yet.
- cesarb 4y agoMost 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.
- naasking 4y ago> we'll look back and see them as the key paradigm shift of this era. You mean the key paradigm shift of the 1970s when they were invented. Only took 50 years for it to go semi-mainstream.
- pdpi 4y agoGoing mainstream is the paradigm shift. OOP was invented in the 60s/70s, but the paradigm shift came in the 80s/90s.
- kajaktum 4y agoThis. 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.
- masklinn 4y ago> 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.
- amelius 4y agoWhat 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.
- Existenceblinks 4y agoCould 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"?
- the__alchemist 4y agoI'll bite - I just googled this, and am having a struggle understanding. Is an Algebraic Data Type (ADT) in rust an enum or tuple? I make heavy use of enums (along with structs) as my program foundations. Am I using ADTs? The third type the query shows are unions, which I'm not familiar with.
- zanellato19 4y agoEnums 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). This is a good explanation: https://jrsinclair.com/articles/2019/algebraic-data-types-what-i-wish-someone-had-explained-about-functional-programming/ https://jrsinclair.com/articles/2019/algebraic-data-types-wh... [1]https://en.wikipedia.org/wiki/Tagged_union https://en.wikipedia.org/wiki/Tagged_union [2]https://en.wikipedia.org/wiki/Product_type https://en.wikipedia.org/wiki/Product_type
- Existenceblinks 4y agoI found https://guide.elm-lang.org/appendix/types_as_sets.html https://guide.elm-lang.org/appendix/types_as_sets.html is friendly describing the what and why.
- belmont_sup 4y agoThey’re enums that can hold additional data types. But they’re additionally great because the compiler does exhaustive pattern matching.
- Existenceblinks 4y agoYou 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?
- valenterry 4y agoTrue. 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.