将F#泛型函数约束为联合类型?

Dut*_*tts 5 generics f# discriminated-union

根据标题,有没有办法将F#泛型函数约束为联合类型?到目前为止,我正在使用:

let toDomain<'T> external: 'T option =
    assert FSharpType.IsUnion(typeof<'T>)
    ...
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用非联合,那么在运行时因System.ArgumentException而失败,但我希望先检查一下.

lat*_*kin 5

没有.

如果你偷看的实施IsUnion,并按照代码了一下,把它归结为检查属性/参数的存在[<CompilationMapping(SourceConstructFlags.SumType)>].

目前,在F#或.NET中都不支持纯属性约束.