Like this and this .
As explained there, you can use reflection or reverse engineer your DU (this is what I would recommend).
Reflection:
open Microsoft.FSharp.Reflection
type Item =
| Normal of string * float32
| Special1 of Item
| Special2 of Item
let innerValue a =
FSharpValue.GetUnionFields (a, a.GetType())
|> snd
|> Seq.head
:?> Item
let rec calcItem (i: Item ) =
match i with
| Normal (_, p) -> p
| specialN -> calcItem (innerValue specialN) + 1.0f
DU Redesign:
type Item =
| Normal of string * float32
| Special of int * Item
let rec calcItem (i: Item ) =
match i with
| Normal (_, p) -> p
| Special (_, g) -> calcItem g + 1.0f
source
share