Search code examples
scalatype-members

Scala type members do not compile


I wrote the following simple example and expected it to be compiled fine:

abstract class TestObject extends App{
  type Type

  def method[F[_]](ft: F[Type], t: Test[F]{
    type Type = TestObject#Type
  }) = t.doSomeAction(ft) //ERROR
}

trait Test[F[_]]{
  type Type
  def doSomeAction(t: F[Type]) = println(t)
}

ideone demo

But the compiler prints the following error message:

Error:(8, 23) type mismatch;
 found   : ft.type (with underlying type F[TestObject.this.Type])
 required: F[t.Type]
    (which expands to)  F[TestObject#Type]
Note: TestObject.this.Type <: t.Type, but type F is invariant in type _.
You may wish to define _ as +_ instead. (SLS 4.5)
  }) = t.doSomeAction(ft)

I do not really understand it since Test#Type = TestObject#Type.


Solution

  • So the problem is that TestObject#Type captures an existential type which is not what you want. you want to ensure that the Type of specific instances line up. you can do this like this:

    https://scalafiddle.io/sf/wpI8iGg/0

    or more commonly with the Aux-Pattern

    https://scalafiddle.io/sf/wpI8iGg/1