.NET常数的F#模式匹配 [英] F# pattern matching of .Net constants

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

问题描述

下一个示例中的代码

open System.Drawing

let testColor c =
    match c with
    | Color.Black -> 1
    | Color.White -> 0
    | _ -> failwith "unexpected color"

不会编译.错误是Error 1 The field, constructor or member 'Black' is not defined.

如何与.Net常量或以大写字母开头的枚举进行模式匹配?

How do I pattern match against .Net constants or enums that start with capital letters?

对于它的价值,编译器是"Microsoft(R)F#2.0 Interactive build 4.0.30319.1".

For what it's worth, the compiler is "Microsoft (R) F# 2.0 Interactive build 4.0.30319.1".

推荐答案

您无法针对任意对象值进行模式匹配.使用if then elsewhen条件:

You can't pattern-match against arbitrary object values. Use an if then else or when conditions:

let testColor c = 
    match c with 
    | c when c = Color.Black -> 1 
    | c when c = Color.White -> 0 
    | _ -> failwith "unexpected color"

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

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