滥用模式匹配 [英] Abusing pattern matching

查看:86
本文介绍了滥用模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自C#,发现自己喜欢F#模式匹配语法,因为它比C#switch更简单,而且更有用.我喜欢尽可能多地使用它,像本例中那样以奇怪的方式使用它是否有性能或其他缺点?

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

推荐答案

在此特定示例中,不会有性能损失.在其他情况下,很可能也不会出现性能下降的情况,但是绝对要确保您必须使用类似ILSpy的代码查看生成的代码.

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.

我还必须补充一点,当您使用F#时,您会发现if/then/else也非常好.在C#中,if/else感觉有点尴尬,因为它不能用作表达式,但是在F#中则不是,因此笨拙很快就消失了.

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

这篇关于滥用模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆