Trying to understand supposed type constraints

Following results

This construction makes the code less general than indicated by type annotations. The type of the variable "P" was limited to the type of "bool".

for the right side of let myValue = and

This code is less general than required by its annotations because an explicit variable of type 'P' cannot be generalized. He was limited to be a "bool".

for the general <'P> in the Value method:

 type MyTypeA<'T> (myObject : 'T) as this = let myValue = this.Value<bool> "SomeBooleanProperty" member this.Value<'P> name = typeof<'T>.GetProperty(name, typeof<'P>).GetValue(myObject, null) :?> 'P` 

However, this compiles just fine and does not give any warnings or errors:

 type MyTypeB<'T> (myObject : 'T) as this = member this.Value<'P> name = typeof<'T>.GetProperty(name, typeof<'P>).GetValue(myObject, null) :?> 'P member this.Method<'P> name = this.Value<'P> name 

What is going on here? Why is the first example a method recognized when assigning a private value, but not as a legally general method?

+6
source share
1 answer

A warning (FS0064) occurs by calling the CheckWarnIfRigid function inside SolveTyparEqualsTyp fun from ConstraintSolver.fs. After the warning is raised, SolveTyparEqualsTyp will continue (since there is no error at the moment) to resolve type restrictions. Comment SolveTyparEqualsTyp:

 /// Add the constraint "ty1 = ty" to the constraint problem, where ty1 is a type variable. /// Propagate all effects of adding this constraint, eg to solve other variables 

This results in error FS0663 for defining the Value member in the OP example. Following error FS0660. For some reason, I ignore, there is some spread.

Perhaps the type conclusion is too aggressive. @jpe and the other comments below the OP question contain more interesting tips.

+2
source

All Articles