Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abusing pattern matching

I come from C# and find myself in love with the F# pattern matching syntax as it's simpler than C# switch and way more useful. I like to use it as much as possible, is there a performance or any other downside to using it in weird ways like in this example?

match 0 with
|_ when a<b -> a
|_ -> b
like image 510
Preza8 Avatar asked May 28 '15 03:05

Preza8


1 Answers

In this particular example, there will be no performance penalty. It is very likely that performance penalty will also be absent in other cases, but to be absolutely sure you'll have to look at the generated code with something like ILSpy.

I must also add that as you use F#, you'll find that if/then/else is also very nice. In C#, if/else feels kinda awkward, because it can't be used as expression, but in F# it is not the case, and so the awkwardness soon disappears.

   let x = if a < b then a else b

It even reads like plain English! :-)

like image 145
Fyodor Soikin Avatar answered Sep 22 '22 16:09

Fyodor Soikin