griin griin - 6 months ago 51
Scala Question

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 delcare ConcreteFooBar like this:

object ConcreteFooBar extends Bar[Int] with Foo {}

Answer Source

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()}")
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download