4 ms·
If Rust leads to more languages for more domains adopting Rust-style enums and pattern matching (algebraic data types / sum types -- this idea predates Rust by
by ralfj 4y ago
If Rust leads to more languages for more domains adopting Rust-style enums and pattern matching (algebraic data types / sum types -- this idea predates Rust by decades, so "modern" is an interesting term to use), I'd call that a big win for everyone. :)
It's pretty tricky to do that in an untyped language. But TypeScript could have this, or maybe even already does?
- cmrdporcupine 4y agoI mean, sure, why are we calling it "Rust-style"; it comes from ML and its descendants and Rust is obviously very influenced by them. When I say "modern" I really mean... finally learned from what "we" were "all" reading about on Lambda The Ultimate 20 years ago about languages designed 40 years ago that were not part of the mainstream until about 5-10 years ago :-) AFAIK TypeScript doesn't have ML-style pattern matching, but its type system is fairly expressive and you can do something "like" it https://dev.to/gvergnaud/bringing-pattern-matching-to-typescript-introducing-ts-pattern-v3-0-o1k https://dev.to/gvergnaud/bringing-pattern-matching-to-typesc...
- ralfj 4y agoI used "Rust-style enums" because if I just say "enum" then people think I mean something else. ;) And then I added the clarification to make sure nobody thinks I would claim that Rust invented Rust-style enums. If I just say "algebraic data types" or "sum types" I fear many people won't know what I mean...
- stormbrew 4y agoI think "discriminated union" is a useful bridge term here, even if it does confuse C programmers due to the conflated meanings of both enum and union in that language. It better represents the concept at an implementation level, and maps it to the C structure that enums more closely resemble in practice.
- int_19h 4y ago> It's pretty tricky to do that in an untyped language. If you mean dynamically typed, then Elixir has it: https://elixir-lang.org/getting-started/case-cond-and-if.html https://elixir-lang.org/getting-started/case-cond-and-if.htm...
- ralfj 4y agoFrom a quick scroll, that seems to be just C-style `select` with `if` guards? What I was referring to is things like `match x { Some(y) => ..., None => ... }`, where there is data that is available only in some variants of the type (like `y`). Without a type, even naming these variants becomes an interesting problem. (Not unsolvable, mind you. But not obvious either, and you lose some of the benefits that algebraic data types have in typed languages.)
- int_19h 4y agoNo, it matches on the structure of data as well as embedded values. E.g. you can match on %{foo: y} vs %{bar: y}.