Hacker News new | ask | show | jobs
by jpz 2037 days ago
It simplifies the expression of if/then/elsif/elsif/else, even if it's not total.

Why not steal it?

2 comments

One reason to not add it might be that it makes the language larger and more complex.

Also, the semantics of match are a bit different than the similar switch, since match does strict type checking and switch doesn't. While this isn't bad as such, it's kinda confusing that the language doesn't behave consistently.

but why all of a sudden languages started to add this? Maybe the function itself needs to be simplified, maybe there is a design issue. i am failing to see how "if else" can be simplified via a synth sugar.
It's a switch statement that's an expression. That's a nice ergonomic improvement when you want to assign the result to a variable. Especially as it allows you to use a non-mutable variable whereas otherwise you'd have to make it mutable.
I wouldn't compare it to a switch statement, because a switch statement - even though it looks similar - has fundamentally different control flow.
one benefit of match over switch is that it uses === and not == internally. So it does not suffer from type coercion problems.