Note that there are some explanatory texts on larger screens.

plurals
  1. POPattern Matching, F# vs Erlang
    primarykey
    data
    text
    <p>In Erlang, you are encouraged not to match patterns that you do not actually handle. For example:</p> <pre><code>case (anint rem 10) of 1 -&gt; {ok, 10} 9 -&gt; {ok, 25} end; </code></pre> <p>is a style that is encouraged, with other possible results resulting in a <code>badmatch</code> result. This is consistant with the "let it crash" philosophy in Erlang.</p> <p>On the other hand, F# would issue an "incomplete pattern matching" in the equivalent F# code, like <a href="https://stackoverflow.com/questions/2829216/is-it-possible-with-simple-f-pattern-matching-transformations-to-ignore-unmatc">here</a>.</p> <p>The question: why wouldn't F# remove the warning, effectively by augmenting every pattern matching with a statement equivalent to</p> <pre><code>|_ -&gt; failwith "badmatch" </code></pre> <p>and use the "let it crash" philosophy?</p> <p><strong>Edit</strong>: 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?</p> <p><strong>Edit</strong>: 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 <a href="http://www.erlang.org/pipermail/erlang-questions/2003-April/008647.html" rel="nofollow noreferrer">let some other process fix the error</a>. 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload