controlflow
controlflow

Reputation: 6737

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.

Upvotes: 7

Views: 843

Answers (3)

Markus Jarderot
Markus Jarderot

Reputation: 89171

You can't write a type-safe function for this. You could however, use the upcast operator instead of your function.

Upvotes: 6

Evgeny Lazin
Evgeny Lazin

Reputation: 9413

This is compiler restriction. Right type of the constraint 'a :> 'b must be of non generic type.

Upvotes: 3

Related Questions