F# でアクティブ パターンを定義して使用する方法 (部分アクティブ パターンや、さまざまな種類の使用可能なパターンを含む) を理解しています。例えば
let (|Big|Small|) animal = if animal.IsBig then Big(animal) else Small(animal)
let f = function | Big(_) -> "big" |Small(_) -> "small
let
ただし、バインド、引数、およびその他の場所でのアクティブ パターンと識別共用体の使用については混乱しています。たとえば、MSDN には次のコードがあります。
let GetSubstring1 (Slice(p0, p1, text)) =
printfn "Data begins at %d and ends at %d in string %s" p0 p1 text
text.[p0..p1]
これは私を少し混乱させます。
特定の問題。私が差別された組合を持っているとしましょう、
type Union = A of int * int | B of int
Union.A
どうにかして のみを受け入れる関数を作成できますか?
let f (A(a, b)) = a + b
この状況では、一致しないパターンがあることがわかります。それを満たす方法はありますか?