Which method of these two pattern matches is preferable?

I'm just curious, these two functions will do the same. But which one should I use?

let fa = match a with b -> a;; let fa = match a with b -> b;; 

Or does it depend only on your preferences?
I feel that the second will be better, but I'm not sure.

+4
source share
2 answers

Performance is reasonable, there is no difference. Style-wise b -> a bit problematic because you have an unused variable b . _ -> a will make more sense. Other than that, it's just a preference.

Personally, I would prefer _ -> a over b -> b , because it does not introduce an additional variable.

PS: I suppose there are more cases in your real code than just b - otherwise you could just write let fa = a .

+8
source

Also, in your specific example, I would rewrite with function

 let f = function | b -> b 
+1
source

All Articles