8 ms·
> Rust's enums and exhaustive match statement are just amazing, and now that I'm using them daily I can barely imagine how I could live without them for so long
by losvedir 4y ago
> Rust's enums and exhaustive match statement are just amazing, and now that I'm using them daily I can barely imagine how I could live without them for so long.
I feel this! I absolutely love rust's fancy enums - i.e. not just a set of constants, but where each variant can be a full fledged type of its own, and how you have to handle every case whenever you're matching on it. It dovetails absolutely wonderfully with non-nullability and errors as values. This is probably the main nice-to-have in a language for me. Unfortunately, I don't really work in a low-level domain so I'm not sure the extra complexity of manually managing memory is worth it to me.
Do any other higher-level languages have rust-style enums with exhaustiveness checking? I only know of Haskell and Ocaml. Actually, I suppose TypeScript has something kind of similar.
- mamp 4y agoF#, Swift
- pdpi 4y agoScala, Kotlin, Haxe, Swift, and several others. The search term you want is "Algebraic Data Types" Erlang has the superpowered version of this - binary pattern matching, that lets you pattern match on a bitstream directly.
- rad_gruchalski 4y agoErlang also has exhaustive pattern matching. To the extent of having to declare catch all arity for functions.
- masklinn 4y ago> Erlang has the superpowered version of this - binary pattern matching, that lets you pattern match on a bitstream directly. Erlang has a super-powered version of the pattern matching bit, but not of what GP actually likes about it: > you have to handle every case whenever you're matching on it because Erlang is dynamically typed. So partial patterns are acceptable, and not entirely uncommon.
- andrewflnr 4y agoBinary pattern matching is very cool, but I wouldn't call it a version of ADT matching.
- vips7L 4y agoDon't forget Java :)
- valenterry 4y agoIt's not there yet, is it?
- vips7L 4y agoPattern matching and exhaustive switch expressions are in preview.
- valenterry 4y agoLet's hope it comes soon then! But right now, I think Java can't be counted in.
- vips7L 4y agoI’m using it
- hota_mazi 4y agoErlang doesn't have enums.
- andrewflnr 4y agoI almost said the same thing, but a tuple starting with an atom is basically the dynamically typed version of an enum. :) You can safely think of ADTs as sugar and typing rules for such tuples, though I believe you do need dependent types to come at it from that direction...
- d3nj4l 4y agoIf you're considering OCaml, you can also take a look at F# and Scala, both of which are languages that are influenced by the ML family but run on more popular host runtimes (the CLR and JVM respectively) that gives them access to a bigger ecosystem.
- Existenceblinks 4y agoBasically ML family languages have them. When you read Rust is ML style, almost 100% sure it's going to have Algebraic Data Types.
- UltraViolence 4y agoYou have to understand where enums originally came from. Initially they were simply #defines in the pre-processor to make code easier to read. Instead of having "magic numbers" all over your code you'd have something that kinda made sense. Then they became part of the C language when they evolved into enums but still remained compatible with int's (you could cast an enum to an int). In that mindset it never occurred to anyone that an enum could have an "internal" value too.