I have a base trait and a child trait. Inside base trait object scope I’ve already provided some instances.
trait BaseTrait[T] {
def f(a:T, b: T): T
}
object BaseTrait {
def apply[T](using ev: BaseTrait[T]): BaseTrait[T] = ev
given IntBaseTrait: BaseTrait[Int] = new BaseTrait[Int] {
def f(a: Int, b: Int): Int = a + b * 10
}
}
trait ChildTrait[T] extends BaseTrait[T] {
def cf: T
}
object ChildTrait {
given c: ChildTrait[Int] = new ChildTrait[Int] {
override def f(a: Int, b: Int): Int = a + b * 10 // I want to use the one provided in BaseTrait object scope
override def cf: T = f(1, 2) + f(3, 4)
}
}
Now in the child trait object scope, I also have a given instance but I also want to extends the functionality from the base trait, instead of duplicating a redundant function at the child trait.
I tried
import BaseTrait._
given c: ChildTrait[Int] = new ChildTrait[Int] with IntBaseTrait[Int] {...}
But this produces Not found: type IntBaseTrait
. which is expected. Any way to achieve this effect?
Thanks