Path dependent types - what happened to the following code?

The following code:

trait Foo { type T val x: T } trait Bar { type F <: Foo { type T <: F } } class C[B <: Bar](val f: B#F) { val x: fT = fx } 

rejected by the Scala compiler (2.11.5) with the following error message:

 error: type mismatch; found : C.this.fxtype (with underlying type C.this.fT) required: this.T val x: fT = fx ^ 

If the declaration of an explicit type is omitted, the type is correctly inferred, in accordance with the exit from the line phase:

 private[this] val x: C.this.fT = C.this.fx; <stable> <accessor> def x: C.this.fT = C.this.x 

The problem also disappears if F inside the border in Bar changes to a type that is not a member of Bar , i.e.

 type F <: Foo { type T <: Foo } 

works correctly.

This is mistake? Or some kind of fundamental misunderstanding on my part? Or some secret function?

+6
source share
1 answer

Not a definite answer, but some observations ...

First, let's see what works:

 class C[B <: Foo](val f: B) { val x: fT = fx } 

therefore, the compiler loses you when using type projection for the value of f . If you “fix” this projection, it also works:

 class D[B <: Bar](val f: B#F) { val g: Foo = f val x: gT = gx } 

I struggled with the F-limited types for a long time until I got them "waterproof." There is something about type parameters compared to type members that do the previous work. For instance:

 trait FooRec[F <: FooRec[F]] extends Foo { type T = F } class E[F <: FooRec[F]](val f: F) { val x: fT = fx } 

Finally, you can also fix a member of type Bar by passing it as a parameter of type:

 class G[F1 <: Foo { type T = F1 }, B <: Bar { type F = F1 }](val f: B#F) { val x: fT = fx } 

Similarly, if you correct the type already in the Bar definition, it works:

 trait Baz { type F <: Foo { type T = F } // stable recursion } class H[B <: Baz](val f: B#F) { val x: fT = fx } 

So in your original definition and application that has upper bounds, it seems not enough. You can probably prove that the compiler is right about its failure, but I don't know how ...

+2
source

Source: https://habr.com/ru/post/982145/


All Articles