by losvedir · 4y ago · 0 comments
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.
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.
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.