I guess what you want is something like that:
trait X {
def apply[T](obj: T): T
}
trait A {
def following(modifier: X) = modifier(this)
}
case class B() extends A
case class C() extends A
object Y extends X {
override def apply[B](obj: B): B = { obj }
override def apply[C](obj: C): C = { obj }
}
object Z extends X {
override def apply[B](obj: B): B = { obj }
override def apply[C](obj: C): C = { obj }
}
Unlucky I don't think you can have two overrided apply methods and because of that it won't compile. If this is possible, then I will be happy to know as well. What you can do for now is to use one apply method with pattern matching:
trait X {
def apply[T](obj: T): T
}
trait A {
def following(modifier: X) = modifier(this)
}
case class B() extends A
case class C() extends A
object Y extends X {
override def apply[T](obj: T): T = {
obj match {
case o: B => obj
case o: C => obj
}
}
}
object Z extends X {
override def apply[T](obj: T): T = {
obj match {
case o: B => obj
case o: C => obj
}
}
}
You could also get exactly the same effect (including syntax) doing it other way around.
In my opinion is much cleaner and easier to understand:
sealed trait X
case class Y() extends X
case class Z() extends X
trait A[T] {
def following(modifier: X): T
}
case class B() extends A[B] {
override def following(modifier: X) = modifier match {
case o: Y => this
case o: Z => this
}
}
case class C() extends A[C] {
override def following(modifier: X) = modifier match {
case o: Y => this
case o: Z => this
}
}