I am learning Scala and I was trying to create a type class to solve the "Every animal eats food, but the type of food depends on the animal" problem. I have an Eats type class with context bounds:
trait Eats[A <: Animal, B <: Edible]
object Eats {
def apply[A, B]: Eats[A, B] = new Eats[A, B] {}
}
with both Animal and Edible being abstract classes. The (reduced) Animal interface looks something like this
abstract class Animal {
type This // concrete type
def eat[A <: Edible](food: A)(implicit e: Eats[This, B]) = // ...
}
My goal is to allow calls in the form of animal.eat(food) only if there is an instance (an implicit value in scope) for the given type of animal and food. For this I created an EatingBehaviour object which basically contains instances for all relations. E. g. to declare that cows eat grass I add the line
implicit val cowEatsGrass = Eats[Cow, Grass]
similar to how you would write instance Eats Cow Grass in Haskell. However, Now i need to specify the abstract type This for all subtypes of the Animal class for the signature in the Animal interface to work:
class Cow extends Animal { type This = Cow }
which is redundant.
Hence my question: Can I somehow initialize the type variable This in Animal so that this always reflects the concrete type, similar to how I could ask for the dynamic type using getClass?
The problem doesn't occur if you pass the first operand
a: Ato a method / class constructor that has the opportunity to infer the externally visible typeA:Here,
EatsOps[A <: Animal]can first infer whatAis, then ineat[B <: Animal]it can infer whatBis, and using information about bothAandBinsert the correct implicit. There are no type members, and nothing has to be done when extendingAnimal.It's a bit of an X-solution to an XY-problem. And, yeah, I reused
Animalinstead ofFood...Update
If you want to access some private methods of a particular
Animalimplementation when invokingeat, the usual way to do this would be to move all the essential functionality into theEatstrait, and then provide instances ofEatsin the companion object of a specificAnimal. For example, here is how we could let aCatdo its uncannyprivatestuff before actually eating aBird:The rest of the code would remain unchanged, except that one doesn't need
e1: Eats[Cat, Bird]any more.