Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern Matching, F# vs Erlang

In Erlang, you are encouraged not to match patterns that you do not actually handle. For example:

case (anint rem 10) of
    1 -> {ok, 10}
    9 -> {ok, 25}
end;

is a style that is encouraged, with other possible results resulting in a badmatch result. This is consistant with the "let it crash" philosophy in Erlang.

On the other hand, F# would issue an "incomplete pattern matching" in the equivalent F# code, like here.

The question: why wouldn't F# remove the warning, effectively by augmenting every pattern matching with a statement equivalent to

|_ -> failwith "badmatch"

and use the "let it crash" philosophy?

Edit: Two interesting answers so far: either to avoid bugs that are likely when not handling all cases of an algebraic datatype; or because of the .Net platform. One way to find out which is to check OCaml. So, what is the default behaviour in OCaml?

Edit: To remove misunderstanding by .Net people who have no background in Erlang. The point of the Erlang philosophy is not to produce bad code that always crashes. Let it crash means let some other process fix the error. Instead of writing the function so that it can handle all possible cases, let the caller (for example) handle the bad cases which are thrown automatically. For those with Java background, it is like the difference between having a language with checked exceptions which must declare everything it will possibly return with every possible exception, and having a language in which functions may raise exceptions that are not explicitly declared.

like image 469
Muhammad Alkarouri Avatar asked Aug 03 '10 19:08

Muhammad Alkarouri


People also ask

What is pattern matching in F#?

Advertisements. Pattern matching allows you to “compare data with a logical structure or structures, decompose data into constituent parts, or extract information from data in various ways”.

Is pattern matching functional?

Pattern Matching: Scala Pattern matching is one of the features of functional programming that make it such an awesome paradigm. This tool is somewhat similar to switch statements you might be familiar with from other languages such as Java or Python, however Scala pattern matching is a much more powerful…

What is pattern matching example?

Pattern matching is used to determine whether source files of high-level languages are syntactically correct. It is also used to find and replace a matching pattern in a text or code with another text/code. Any application that supports search functionality uses pattern matching in one way or another.

What does F# Let mean?

let is the F# keyword used to bind any value to a name, it's used to bind the so called primitive types such as a string or an integer , to bind to a function or more complex structures such as arrays or records.


1 Answers

F# (and other languages with pattern matching, like Haskell and O'Caml) does implicitly add a case that throws an exception.

In my opinion the most valuable reason for having complete pattern matches and paying attention to the warning, is that it makes it easy to refactor by extending your datatype, because the compiler will then warn you about code you haven't yet updated with the new case.

On the other hand, sometimes there genuinely are cases that should be left out, and then it's annoying to have to put in a catch-all case with what is often a poor error message. So it's a trade-off.

In answer to your edit, this is also a warning by default in O'Caml (and in Haskell with -Wall).

like image 123
GS - Apologise to Monica Avatar answered Sep 20 '22 14:09

GS - Apologise to Monica