I am trying to use a specific member of a discriminated union as a parameter type. For example:
type SomeUnion =
| A of int * int
| B of string
type SomeType(A(i, j)) =
member this.I = i
member this.J = j
let a = A(10, 20)
let instance = SomeType(a)
but this is illegal syntax, and complains with "Unexpected symbol '(' in type definition" for SomeType's param list. This is valid syntax:
let doSomethingWithA (A(i, j)) = i + j
but the type signature is SomeUnion -> int
rather than A -> int
, and it complains about incomplete pattern match (understandable given the signature).
So is this possible? I believe in F# union members are compiled to CLR classes, so seems theoretically possible, but is it practically (i.e. without using something like reflection)? Otherwise I suppose you have to do the manual OOP way which is more verbose, and can't guarantee complete matches.