Rewrite some C# generic code into F#
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. 开发者_Python百科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.
This isn't possible in F#. See How do I translate a `where T : U` generic type parameter constraint from C# to F#? and http://cs.hubfs.net/forums/thread/10534.aspx.
This is compiler restriction. Right type of the constraint 'a :> 'b must be of non generic type.
精彩评论