I'm trying to rewrite generic code like this (C#):
U Upcast<T, U>(T x) where T : U { return x; }
In F#:
let ucast<'T, 'U when 'T :> 'U> (x: 'T) = x :> 'U
But F# constraint solving works different than C# and compiler outputs a bunch of typing errors:
error FS0698: Invalid constraint: the type used for the constraint is sealed, which means the constraint could only be satisfied by at most one solution
warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type ''U'.
error FS0663: This type parameter has been used in a way that constrains it to always be ''U'
error FS0013: The static coercion from type 'U to 'U
involves an indeterminate type based on information prior to this program point. Static coercions are not allowed on some types. Further type annotations are needed.error FS0661: One or more of the explicit class or function type variables for this binding could not be generalized, because they were constrained to other types
Please, explain me how to correctly rewrite C# code above and why F# version I've wrote doesn't compiles.
You can't write a type-safe function for this. You could however, use the upcast
operator instead of your function.
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