scalagenericstypesself-type

Scala self-type and generic class


abstract class Bar[M] {
  def print(t: M): Unit = {
    println(s"Bar: ${t.getClass()}")
  }
}

trait Foo[M] {
  this: Bar[M] =>
  def print2(t: M): Unit = {
    println(s"Foo: ${t.getClass()}")
  }
}

object ConcreteBar extends Bar[Int] with Foo[Int] {}
object ConcreteFooBar extends Bar[Int] with Foo[Int] {}

object Test {
  def main(args: Array[String]): Unit = {
    ConcreteBar.print(1)
    ConcreteFooBar.print2(1)
  }

In the example above, is there a way so that we don't have to repeat the type in the self-typed "bar" trait? Therefore we could declare ConcreteFooBar like this:

object ConcreteFooBar extends Bar[Int] with Foo {}

Solution

  • You can use an abstract type instead of a type parameter for Foo, like this:

    abstract class Bar[M] {
      type Base = M
      def print(t: M): Unit = {
        println(s"Bar: ${t.getClass()}")
      }
    }
    
    trait Foo {
      type Base
      def print2(t: Base): Unit = {
        println(s"Foo: ${t.getClass()}")
      }
    }