Generic F# function: How to get the Type of an F# Discriminated Union?

前提是你 提交于 2019-12-01 06:52:22

I think that the additional boxing that you introduce for the 2nd parameter of Makeunion throws the type inference off the track and in fact, the function isn't generic anymore. Either annotate the argument or remove the box.

let toDomain<'T> external : 'T =
    let found = FSharpType.GetUnionCases(typeof<'T>) |> Array.find (fun t -> t.Name = external.GetType().Name) 
    FSharpValue.MakeUnion(found, [| external |]) :?> 'T 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!