.Net 常量的 F# 模式匹配
下一个示例中的代码
open System.Drawing
let testColor c =
match c with
| Color.Black -> 1
| Color.White -> 0
| _ -> failwith "unexpected color"
无法编译。错误为错误 1 未定义字段、构造函数或成员“Black”
。
如何针对以大写字母开头的 .Net 常量或枚举进行模式匹配?
就其价值而言,编译器是“Microsoft (R) F# 2.0 Interactive build 4.0.30319.1”。
The code in the next example,
open System.Drawing
let testColor c =
match c with
| Color.Black -> 1
| Color.White -> 0
| _ -> failwith "unexpected color"
doesn't compile. The error is Error 1 The field, constructor or member 'Black' is not defined
.
How do I pattern match against .Net constants or enums that start with capital letters?
For what it's worth, the compiler is "Microsoft (R) F# 2.0 Interactive build 4.0.30319.1".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
扩展 Brian 的答案,模式匹配与 switch 语句是不同的野兽。它们测试并分解输入的结构,而不是测试对象的相等性。但是,如果在整个程序中经常使用将颜色划分为黑色、白色和其他颜色,则活动模式可能是一个选择。对于一次性“样板”成本,它们可以让您围绕正在操作的对象定义一种结构。例如,
或者,如果您同样只处理黑色和白色,但您总是希望黑色计算为 1,白色计算为 0,那么您可以使用部分活动模式:
更一般地,您甚至可以模拟 switch 语句使用通用的部分活动模式:
Expanding upon Brian's answer, pattern-matches are a different beast than switch statements. They test and decompose the structure of an input rather than test object equality. But Active Patterns may be an option if the division of colors into Black, White and Other will be used often throughout your program. For a one-time "boiler-plate" cost, they let you define a structure around the object you are manipulating. For example,
Or, if you are likewise dealing with only Black and White, but you always want Black to evaluate as 1 and White to evaluate as 0, then you can use Partial Active Patterns:
More generally, you can even emulate a switch statement using a generic Partial Active Pattern:
您不能对任意对象值进行模式匹配。使用
if then else
或when
条件:You can't pattern-match against arbitrary object values. Use an
if then else
orwhen
conditions: