nicolas
nicolas

Reputation: 9805

Flexible type and generic parameters

I wonder why the second line telling me that the flexible type has been constrained. Obviously one can go around it, but is there some lesson to take from this ?

type DataTable with
    static member FromObjArray     (input : seq<#seq<'T>>) =  ()  //keep the flexibility
    static member FromObjArray<'T> (input : seq<#seq<'T>>) =  ()  //constrained to seq

Upvotes: 2

Views: 352

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243041

I think the flexible type is constrained in the second case because you explicitly declared the method as a generic method with just one type parameter 'T. A flexible type is desugared to another type parameter and the compiler cannot add that (hidden) type parameter if you make the parameters explicit.

The first declaration corresponds to something like this:

static member FromObjArray<'T, 'S when 'S :> seq<'T>> (input : seq<'S>) =  ()

If you explicitly say that the method only takes type parameter 'T, then the compiler cannot generate method like this (because it needs to add 'S for the type derived from sequence).

Upvotes: 3

Related Questions