0 people like it.

Why do I get a warning?

TagUnionCaseReflection.fs(17,39): warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'TagUnionType has been constrained to be type 'obj'. System.ArgumentException: Type 'System.Object' is not an F# union type. Parameter name: unionType at Microsoft.FSharp.Reflection.Impl.checkUnionType(Type unionType, BindingFlags bindingFlags) at Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(Type unionType, FSharpOption`1 bindingFlags) at FSI_0034.TagUnionCaseReflection`1.GetAll() in C:\home\projects\fsharp.playground\Smoothdev.Utils\src\smoothdev.FSharp.Reflection\TagUnionCaseReflection.fs:line 17 at .$FSI_0034.main@() Stopped due to error

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
open Microsoft.FSharp.Reflection

type TagUnionCase<'TTag> =  
  { CaseInfo: UnionCaseInfo
  ; Value: 'TTag
  }

type TagUnionCaseReflection<'TTag> =
    
    static member construct caseInfo =
      FSharpValue.MakeUnion(caseInfo, [||]) :?> 'TTag

    static member GetAll() = 
     
      FSharpType.GetUnionCases(typeof<'TagUnionType>)
      |> Array.map (fun c -> { CaseInfo = c; Value = TagUnionCaseReflection<'TTag>.construct c})

type Foos = Foo | Bar | Baz

TagUnionCaseReflection<Foos>.GetAll()
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Reflection
type TagUnionCase<'TTag> =
  {CaseInfo: UnionCaseInfo;
   Value: 'TTag;}

Full name: Script.TagUnionCase<_>
TagUnionCase.CaseInfo: UnionCaseInfo
type UnionCaseInfo
member GetCustomAttributes : unit -> obj []
member GetCustomAttributes : attributeType:Type -> obj []
member GetCustomAttributesData : unit -> IList<CustomAttributeData>
member GetFields : unit -> PropertyInfo []
member DeclaringType : Type
member Name : string
member Tag : int

Full name: Microsoft.FSharp.Reflection.UnionCaseInfo
TagUnionCase.Value: 'TTag
type TagUnionCaseReflection<'TTag> =
  static member GetAll : unit -> TagUnionCase<'TTag> []
  static member construct : caseInfo:UnionCaseInfo -> 'TTag

Full name: Script.TagUnionCaseReflection<_>
static member TagUnionCaseReflection.construct : caseInfo:UnionCaseInfo -> 'TTag

Full name: Script.TagUnionCaseReflection`1.construct
val caseInfo : UnionCaseInfo
type FSharpValue =
  static member GetExceptionFields : exn:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetRecordField : record:obj * info:PropertyInfo -> obj
  static member GetRecordFields : record:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetTupleField : tuple:obj * index:int -> obj
  static member GetTupleFields : tuple:obj -> obj []
  static member GetUnionFields : value:obj * unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo * obj []
  static member MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
  static member MakeRecord : recordType:Type * values:obj [] * ?bindingFlags:BindingFlags -> obj
  static member MakeTuple : tupleElements:obj [] * tupleType:Type -> obj
  static member MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:BindingFlags -> obj
  ...

Full name: Microsoft.FSharp.Reflection.FSharpValue
static member FSharpValue.MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?allowAccessToPrivateRepresentation:bool -> obj
static member FSharpValue.MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:System.Reflection.BindingFlags -> obj
static member TagUnionCaseReflection.GetAll : unit -> TagUnionCase<'TTag> []

Full name: Script.TagUnionCaseReflection`1.GetAll
type FSharpType =
  static member GetExceptionFields : exceptionType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetFunctionElements : functionType:Type -> Type * Type
  static member GetRecordFields : recordType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetTupleElements : tupleType:Type -> Type []
  static member GetUnionCases : unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo []
  static member IsExceptionRepresentation : exceptionType:Type * ?bindingFlags:BindingFlags -> bool
  static member IsFunction : typ:Type -> bool
  static member IsModule : typ:Type -> bool
  static member IsRecord : typ:Type * ?bindingFlags:BindingFlags -> bool
  static member IsTuple : typ:Type -> bool
  ...

Full name: Microsoft.FSharp.Reflection.FSharpType
static member FSharpType.GetUnionCases : unionType:System.Type * ?allowAccessToPrivateRepresentation:bool -> UnionCaseInfo []
static member FSharpType.GetUnionCases : unionType:System.Type * ?bindingFlags:System.Reflection.BindingFlags -> UnionCaseInfo []
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val c : UnionCaseInfo
type Foos =
  | Foo
  | Bar
  | Baz

Full name: Script.Foos
union case Foos.Foo: Foos
union case Foos.Bar: Foos
union case Foos.Baz: Foos

More information

Link:http://fssnip.net/pJ
Posted:10 years ago
Author:
Tags: warning , generics