我使用反射(例如How to enumerate a discriminated union in F#?)计算F#中某些受歧视的联合中的值。我想使用我从使用反射生成的值来生成不同的记录类型,这些记录类型是由我所枚举的有区别的联合组成的,但我不确定如何将类型UnionCaseInfo转换为实际的联合案例。是否有可能进行这样的演员表演?下面的代码正好代表了我想要做的事情(区别联合中的值是不同的,变量名也是如此)。我知道我可以使用枚举,但我宁愿不使用它们而不是歧视的联合。
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
我重新将我的代码重新考虑到以下内容:
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
这种新的重新分解结合了反思,通过歧视的联盟进行列举,同时允许我生成由这些受歧视的联盟组成的不同记录类型。我还发现了两个一个一个错误,它们在重新分解的代码中被修复。
答案 0 :(得分:9)
说实话,我更喜欢在静态方法中手动枚举所有联合案例,而不是通过反思来创建它们。
正如@John所说,使用FSharpValue.MakeUnion还需要再做一步:
let allUnionCases<'T>() =
FSharpType.GetUnionCases(typeof<'T>)
|> Array.map (fun case -> FSharpValue.MakeUnion(case, [||]) :?> 'T)
在while
循环中,您应该使用=
而不是Equals
,并使用没有完全限定名称的联合案例(shape.Other
)。
/// 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 }