30 ms·
Out of curiosity, why match expressions became popular for non functional languages? Without pattern matching on datatypes via cons expression (x::xs matching)
by yetkin 6y ago
Out of curiosity, why match expressions became popular for non functional languages? Without pattern matching on datatypes via cons expression (x::xs matching) it is just another kind of switch or if/else. Why do people promote this?
here a nice comment on this topic:
https://ocaml.org/learn/tutorials/data_types_and_matching.html#Pattern-matching-on-datatypes https://ocaml.org/learn/tutorials/data_types_and_matching.ht...
"So one Really Cool Feature of functional languages is the ability to break apart data structures and do pattern matching on the data. This is again not really a "functional" feature - you could imagine some variation of C appearing which would let you do this, but it's a Cool Feature nonetheless"
- jpz 6y agoIt simplifies the expression of if/then/elsif/elsif/else, even if it's not total. Why not steal it?
- yetkin 6y agobut 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.
- beberlei 6y agoone benefit of match over switch is that it uses === and not == internally. So it does not suffer from type coercion problems.
- nicoburns 6y agoIt'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.
- chmod775 6y agoI wouldn't compare it to a switch statement, because a switch statement - even though it looks similar - has fundamentally different control flow.
- deleted 6y ago[deleted]
- arp242 6y agoOne 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.
- muglug 6y agoPattern matching has been proposed in a separate RFC: https://wiki.php.net/rfc/pattern-matching https://wiki.php.net/rfc/pattern-matching
- deergomoo 6y agoOne reason for PHP specifically is that in PHP switch performs type coercion whereas match does not.