I am enumerating through values in some discriminated unions in F# using reflection (e.g. How to enumerate a discriminated union in F#?). I want to use the values that I got from using reflection in generating different record types that are composed from the discriminated unions that I am enumerating through but I am unsure of how to cast the type UnionCaseInfo to an actual union case. Is it possible to perform such a cast? The code below represents exactly what I am trying to do (the values in the discriminated union are different and so are the variable names). I am aware I could use enumeration but I would prefer to not to use them instead of discriminated unions.
open System
open Microsoft.FSharp.Collections
open Microsoft.FSharp.Reflection
let GetUnionCaseName (x:'a) =
match FSharpValue.GetUnionFields(x, typeof<'a>) with
| case, _ -> case.Name
type shape =
| Square
| Circle
| Triangle
| Other
type color =
| Black
| Red
| Blue
| Green
| White
type coloredShape = { Shape: shape; Color: color }
let shapeCases = FSharpType.GetUnionCases typeof<shape>
let colorCases = FSharpType.GetUnionCases typeof<color>
let boardOfRelevantPossibilities = Microsoft.FSharp.Collections.Array2D.init<coloredShape> 5 3 (fun x y -> {Shape = Other; Color = Black})
let OtherShape = GetUnionCaseName(shape.Other)
let rand = Random()
for shapeCase in shapeCases do
// Is there a way to do the following comparison this without using string comparisons
if not (shapeCase.Name.Equals OtherShape) then
for colorCase in colorCases do
let mutable addedToBoard = false
while not addedToBoard do
let boardRowIndex = rand.Next(0,4)
let boardColumnIndex = rand.Next(0,2)
if boardOfRelevantPossibilities.[boardRowIndex,boardColumnIndex].Shape.Equals shape.Other then
addedToBoard <- true
// I want to utilize colorCase instead of other and shapeCase instead of black
boardOfRelevantPossibilities.[boardRowIndex,boardColumnIndex] <- {Shape = Other; // Shape should be determined by shapeCase instead of Other
Color = White } // Color should be determined by colorCase instead of White
Console.ReadKey() |> ignore
I re-re-factored my code to the following:
open System
open Microsoft.FSharp.Collections
open Microsoft.FSharp.Reflection
let allUnionCases<'T>() =
FSharpType.GetUnionCases(typeof<'T>)
|> Array.map (fun case -> FSharpValue.MakeUnion(case, [||]) :?> 'T)
type shape =
| Square
| Circle
| Triangle
| Other
type color =
| Black
| Red
| Blue
| Green
| White
type coloredShape = { Shape: shape; Color: color }
let shapeCases = FSharpType.GetUnionCases typeof<shape>
let colorCases = FSharpType.GetUnionCases typeof<color>
let numberOfRows = 5
let numberOfColumns = 3
let boardOfRelevantPossibilities = Microsoft.FSharp.Collections.Array2D.init<coloredShape> numberOfRows numberOfColumns (fun x y -> {Shape = Other; Color = Black})
let rand = Random()
for shapeCase in allUnionCases<shape>() do
// No string comparison anymore
if shapeCase <> shape.Other then
for colorCase in allUnionCases<color>() do
let mutable addedToBoard = false
while not addedToBoard do
let boardRowIndex = rand.Next(0,numberOfRows)
let boardColumnIndex = rand.Next(0,numberOfColumns)
if boardOfRelevantPossibilities.[boardRowIndex,boardColumnIndex].Shape.Equals shape.Other then
addedToBoard <- true
// utilizing colorCase and shapeCase to create records to fill array
boardOfRelevantPossibilities.[boardRowIndex,boardColumnIndex] <- {Shape = shapeCase; Color = colorCase }
printfn "%A" boardOfRelevantPossibilities
Console.ReadKey() |> ignore
This new re-factoring incorporates reflection to enumerate through discriminated unions while allowing me to generate different record types composed of those discriminated unions. I also caught two off-by-one errors, they are fixed in the re-factored code.
To be honest, I prefer to enumerate all union cases manually in a static method rather than create them through reflection.
As @John said, you need one more step using FSharpValue.MakeUnion:
let allUnionCases<'T>() =
FSharpType.GetUnionCases(typeof<'T>)
|> Array.map (fun case -> FSharpValue.MakeUnion(case, [||]) :?> 'T)
In the while
loop, you should use =
instead of Equals
and use union cases (shape.Other
) without fully-qualified names.
/// Use type params for clarity; type inference should work fine without them
for shapeCase in allUnionCases<shape>() do
for colorCase in allUnionCases<color>() do
let mutable addedToBoard = false
while not addedToBoard do
let r = rand.Next(0,4)
let c = rand.Next(0,2)
if boardOfRelevantPossibilities.[r, c].Shape = Other then
addedToBoard <- true
boardOfRelevantPossibilities.[r, c] <- { Shape = shapeCase;
Color = colorCase }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With