Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share generic between traits

I have two traits

trait Base[A]

trait SometimesUsedWithBase[A] {
    this: Base[A] =>
}

I then use these with a class

class StringThing extends Base[String] with SometimesUsedWithBase[String]

It would be great if I didn't have to define SometimesUsedWithBase's type, and instead it somehow understands that it's using the type defined in Base so that it looks like:

class StringThing extends Base[String] with SometimesUsedWithBase

Is this possible?

like image 770
MintyAnt Avatar asked Apr 27 '16 15:04

MintyAnt


1 Answers

You should be able to do something like this.

trait Base[A] {
  type BaseType = A
}

trait SometimesUsedWithBase {
  this: Base[_] =>
  def someFunction: BaseType
}

class StringThing extends Base[String] with SometimesUsedWithBase {
  def someFunction: String = ""
}
like image 193
StuartMcvean Avatar answered Oct 22 '22 16:10

StuartMcvean