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>
したがって、確かに、それらは同じ有効なパターンのセットを受け入れる必要があります