Why usage of type members bound to class value parameters generates a "type mismatch" error? For example:
scala> class A { type t }
defined class A
scala> class B(val a: A)
defined class B
scala> val aInt = new A { type t = Int }
aInt: A{type t = Int} = $anon$1@6ccc7368
scala> val b = new B(aInt)
b: B = B@834518
scala> val i: b.a.t = 1
<console>:11: error: type mismatch;
found : Int(1)
required: b.a.t
val i: b.a.t = 1
^
The strange thing is that when I use a value which is not defined as a class parameter everything works fine:
scala> abstract class C { val a: A }
defined class C
scala> val c = new C { val a = aInt }
c: C{val a: A{type t = Int}} = $anon$1@1e815aad
scala> val i: c.a.t = 1
i: c.a.t = 1
What is the reason for such behaviour?
Seems like I've understood what's happening. In the example with class
Bthe valueais converted to typeAwhich has no definite value for typet. In the example with abstract classCthe instancecoverrides the value ofawithaIntwhich has a concrete type fort. In other words,cis not an instance ofC: it's an instance of its anonymous subclass.If I change the example stating the type of
casCexplicitly then I get the same error as in the first case: