Scala cannot prove that type-dependent types are equal, although that should be clear?

trait A {
  type Foo
  def bar: Foo = B(this).bar
}

case class B(a: A) extends A {
  type Foo = a.Foo
}

      

I realize this example is a bit contrived, but isn't it a compilation? I am getting the following error:

<console>:9: error: type mismatch;
 found   : _5.a.Foo where val _5: B
 required: A.this.Foo
             def bar: Foo = B(this).bar

      

I realize my example is weird, but it should be clear that the Foos are the same, no? Since "this" is the path that defines Foo!

Should I just throw it?

+3


source to share


2 answers


The following works fine:

trait A {
  type Foo
  def bar: Foo = B(this).bar
}

def B(a: A): A { type Foo = a.Foo } = new A {
  type Foo = a.Foo
}

      



So it looks like the compiler is simply not smart enough to keep track of what is B

Foo

. Maybe you could convince someone on the compiler team that this is a bug, but I would not hold my breath.

+1


source


One way to make this work is to give the initial type Foo. It seems that type inference without this is complex type inference that is inconsistent with what you expect.

(Note: if anyone knows more about the details, I would like to know more about this as well.)



trait A {
  type Foo = Int
  def bar: Foo = B(this).bar
}

case class B(a: A) extends A {
  override type Foo = a.Foo
}

      

0


source







All Articles