2 ms·
Agreed regarding the "or" - rejecting consistency here just because other languages do it differently is weird. > it's not clear what it makes more succinct th
by alpaca128 6y ago
Agreed regarding the "or" - rejecting consistency here just
because other languages do it differently is weird.
> it's not clear what it makes more succinct than the equivalent `elif` statements
But pattern matching is shorter than if-else in many cases and
can actually differentiate between structures which makes it
very flexible. It's like switch-case should have been from the
start imho.
As an example, in Rust I often do comparisons between certain
values by simply matching a tuple of them:
match (foo, bar) {
(x, x) => true, // foo == bar
(Foo::VariantA(42), _) => unreachable!(), // has to be a bug
(Foo::VariantA(a), _) => a < 0,
_ => false,
}
This is of course a rather simple example, but I hope it still
conveys what I like so much about this kind of feature. It
doesn't do more than an if-else chain, but under the right
circumstances it really shines. Just like list comprehensions
don't add any new functionality, but the filtering and mapping
features are often simply more expressive for what you want to
do.
Although I'm not sure what to think of Python's preference to
put the condition and code in different lines; maybe that's
optional, which could make it more succinct when the situation
permits it.