As per the title, is there any way to constrain an F# generic function to a union type? So far I am using:
let toDomain<'T> external: 'T option =
assert FSharpType.IsUnion(typeof<'T>)
...
Which fails at runtime with a System.ArgumentException if I attempt to use a non-union, but I would prefer the check earlier.
No.
If you peek at the implementation of IsUnion
and follow the code a bit, it boils down to checking for the presence of the attribute/argument [<CompilationMapping(SourceConstructFlags.SumType)>]
.
For now there is no support for purely attribute-based constraints, either in F# or in .NET.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.