If you are happy to change the method name from == to something else, we can do this:
trait Fruit { type FruitType <: Fruit val color:String def === (fruit:FruitType) = this.color == fruit.color } case class Orange(color:String) extends { type FruitType = Orange } with Fruit case class Apple(color:String) extends {type FruitType = Apple } with Fruit
Then, if we compare Apples with oranges, we get:
Apple("red") === Orange("red") <console>:11: error: type mismatch; found : Orange required: Apple Apple("red") === Orange("red")
and apples with apples of the same color, we get:
Apple("red") === Apple("red") res10: Boolean = true
and Apples with Apples of a different color, we get:
Apple("green") === Apple("red") res11: Boolean = false
ssanj source share