Abusing pattern matching

六月ゝ 毕业季﹏ 提交于 2019-12-07 02:28:53

问题


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

回答1:


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! :-)



来源:https://stackoverflow.com/questions/30496550/abusing-pattern-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!