我对观察到的F#代码行为感到非常困惑,以下内容来自交互式会话:
Microsoft (R) F# 2.0 Interactive build 4.0.40219.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> type foo = Foo of (string * int);;
type foo = | Foo of (string * int)
> let f = Foo ("bar",42);;
val f : foo = Foo ("bar", 42)
> match f with Foo x -> x;;
val it : string * int = ("bar", 42)
> type bar = Bar of string * int;;
type bar = | Bar of string * int
> let b = Bar ("baz",21);;
val b : bar = Bar ("baz",21)
> match b with Bar x -> x;;
match b with Bar x -> x;;
-------------^^^^^
stdin(7,14): error FS0019: This constructor is applied to 1 argument(s) but expects 2
>
我认为使用单个变量对Foo和Bar进行模式匹配应该是有效的,这似乎是显而易见的-因此,我想知道是否有人知道这种奇怪行为的原因,或者像我一样认为这是一个错误。
更新:
仅澄清,构造函数 Foo
和 Bar
的报告类型如下:
> Foo;;
val it : string * int -> foo = <fun:clo@14-1>
> Bar;;
val it : string * int -> bar = <fun:clo@13>
因此,他们应该接受相同的有效模式集。