2

I have read the answers from How to enumerate a discriminated union in F#?

And I like the solution suggested: solution

However, I am not sure how to write a function where I would pass the discriminated union as argument?

let testDisUnion a =
    SimpleUnionCaseInfoReflection.AllCases<a> 
    |> Seq.iter (fun (_, instance) -> printfn "name: %s" instance)

Thank you

TheQuickBrownFox
  • 10,544
  • 1
  • 22
  • 35
Jeff_hk
  • 421
  • 3
  • 12

1 Answers1

3

This is how to use a type argument as dumetrulo suggested:

let testDisUnion<'a> =
    SimpleUnionCaseInfoReflection.AllCases<'a> 
    |> Seq.iter (fun (_, instance) -> printfn "name: %A" instance)

testDisUnion<MyType>
//name: A
//name: B

type MyType = A | B

The type argument <'a> is being passed from your "function" to the AllCases "function". I write function in quotes because although there are no proper parameters, the type argument is a type of input to the function, which means that the value is only evaluated when you "call" it with a type argument.

TheQuickBrownFox
  • 10,544
  • 1
  • 22
  • 35