这是一些代码:
type Test() =
static member SomeValue =
"AAA"
static member test1 (a) =
match a with
| Test.SomeValue -> "a"
| _ -> "b"
static member test2 (a) =
if a = Test.SomeValue then
"a"
else
"b"
当test2编译时,test1版本将不会编译([FS0039]类型“ Test”没有定义字段,构造函数或成员“ SomeValue”。)
由于SomeValue没有任何参数,因此它将被评估一次,使其成为常数。在这种情况下,编译器为什么不接受它?
在该test1
版本中,您正在使用模式匹配,因此必须在管道符号()后提供一个模式|
。
不过Test.SomeValue
是一个属性,而不是一个模式,根据微软的文档。
因此,例如,要在代码中使用模式匹配,可以将其标记SomeValue
为Literal
:
type Test() =
[<Literal>] static let SomeValue = "AAA"
static member test1 (a) =
match a with
| SomeValue -> "a"
| _ -> "b"
static member test2 (a) =
if a = SomeValue then
"a"
else
"b"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句