简体   繁体   中英

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.

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 is compiler restriction. Right type of the constraint 'a :> 'b must be of non generic type.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM