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に一致するそのパターン私には明らかと思われます1つの変数を持つBarが有効である必要があります。だから、誰かがこの奇妙な振る舞いの理由を知っていたのか、あなたが私を好きなのであればそれをバグだと思っています。
更新: だけ明確にする、コンストラクタFoo
とBar
の報告タイプは次のとおりです。
> Foo;;
val it : string * int -> foo = <fun:[email protected]>
> Bar;;
val it : string * int -> bar = <fun:[email protected]>
確かに、彼らは有効なパターンの同じセットに
非常に徹底的な回答をいただきありがとうございます。また、関連する言語仕様を指摘していただきありがとうございます。しかし、私はそれが意図的だったことを今、さらに深刻な怪物と考えているのではないかと恐れています! :-) – plc
@plc - 私は違いの理由は、C#のような他の言語とのinteropの境界を制御することだと思うだろう。その場合は、あらかじめ作成されたタプルを渡すか、構成項目を個別に渡すことによってDUインスタンスを作成する必要があるかどうかを気にする必要があります。 – kvb