You can use static member constraints, here is a "short" example:
type Explicit = static member inline ($) (_:byte , _:Explicit) = byte static member inline ($) (_:sbyte, _:Explicit) = sbyte static member inline ($) (_:int16, _:Explicit) = int16 static member inline ($) (_:int32, _:Explicit) = int
As stated in the comments, you can use the explicit function from F # + , which covers all cases where there is an explicit operator. Here is an example code .
Now, if you look at the source code for this function, which is defined in another project (FsControl), you will find an even more complicated workaround.
You may wonder why, so here is the long answer:
In theory, it should be possible to use one call that calls the op_Explicit member, but this will only work when that member really exists, which is not the type of native numbers.
In these cases, the F # compiler uses a function, commonly called "simulated elements," which is implemented using static optimizations but is not available outside the source code of the F # compiler.
So F # + uses a different function: overload resolution, as in the code example that I showed you, but it uses extra overload as a general case for those members that really contain the static member op_Explicit .
source share