4 ms·
I've recently started learning Erlang and Elixir and the feature that made me fall in love is pattern matching, combined with great support for recursion. Yes,
by rubiquity 13y ago
I've recently started learning Erlang and Elixir and the feature that made me fall in love is pattern matching, combined with great support for recursion. Yes, some people will say pattern matching is just the same as if/switch statements but I think it's so much better. Pattern matching let's you push that conditional logic one level lower (similar to polymorphism and duck typing) and it makes reasoning about code so much easier.
- _mikz 13y agoAnd do they support this kind of smart warnings on unmatched possibilities?
- alco 13y agoThere are no enums in Erlang and Elixir. If you have a case expression and there is no match with the given value, you'll get a run time error. This is deliberate and is part of the general approach to error handling in those languages. Statically typed languages like Haskell and Rust require the programmer to list all possible cases when matching on a value.
- IsTom 13y agoHaskell doesn't require you to do that by default. There is a warning flag for that, but in general language is okay with defining partial functions, more so than most languages (because laziness lets you cheat death).
- kibwen 13y ago> Statically typed languages like Haskell and Rust require > the programmer to list all possible cases when matching > on a value. This isn't a consequence of static vs. dynamic, it's just a question of philosophy. In fact, Rust didn't use to require you exhaustively list out all cases, though it does today in order to help catch errors at compile-time rather than at runtime. I should also mention that if yo don't want to list out all variants, that's easily done by using the _ pattern (the "ignore this" pattern): match some_direction { East => print("You're going east!"), _ => print("You're going in some direction that isn't east!") }
- alco 13y agoWhat I mean is that it's not possible in a dynamically typed language to enforce at compile time enumeration of all possible cases for a variable, the type of which will be known at run time.
- dozzie 13y agoActually, it's done some other way. You have Dialyzer, which tells you that your case clause will never match the data you provided or the case will surely blow up because clauses didn't catch some possibility that will occur.
- dozzie 13y agoPattern matching is 1) conditional on values, 2) conditional on structure (not something you can get in imperative languages), 3) data extractor from data structures, all at the same time. Yes, it's helluva convenient. Pity it didn't make it to imperative languages (yet).
- DanWaterworth 13y agoIt's in rust.
- cromwellian 13y agoScala has it.