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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With