mardi 14 mars 2023

Identify DU level when partially applied

Suppose we have a DU with 3x levels parameterised by reference types as shown:

type DU =
    | X of Object
    | Y of Object
    | Z of Object

Noting that the GetUnionCases function within the FSharp.Reflection.FSharpType namespace can, when provided with the type information for a DU, return a UnionCaseInfo array.

I am looking for a function f: ('T -> DU) -> UnionCaseInfo, such that:

  • f X = UnionCaseInfo for level X.
  • f Y = UnionCaseInfo for level Y.
  • ...and so on.

Assuming a many-to-1 mapping between level and type parameter as shown at the start... Is this even possible?

If the DU levels were parameterless, the GetProperties() member provided by typeof<DU> contains singleton instances that can, via PropertyInfo.Name, be tied back to a corresponding UnionCaseInfo.

Alternatively... If the parameter type was different across all levels, we could do something akin to:

let x = X

Subsequently calling x.GetType().GetMethods() shows a MethodInfo for Invoke(<some type>); using <some type> we could tie it back to the underlying level.

Unfortunately, neither of these methods (as far as I am aware) are suitable for this particular example!





Aucun commentaire:

Enregistrer un commentaire