dimanche 21 octobre 2018

how pass implicit parameter to pimp'ed type

I want to pass implicit parameter to my pimp'ed type as I am doing in the actual type. But since implicit classes take only single parameter, I don't have a way to implicitly pass parameter to my new type.

package com.abc.xyz

class Pet(val petName: String){
  override def toString = petName
}

class PetLover(val name: String)(implicit val pet: Pet) {
  def showLove = s"${name} shows love, ${pet} waves tail"
}

I am trying to pimp PetLover into PetTrainer and add new method train

package com.abc

import com.abc.xyz.PetLover

package object mno {
  implicit class PetTrainer(val trainer: PetLover) extends AnyVal{
    def train = s"${trainer.name} show hand," //${pet} high fives"
  }
}

What can I do to make Pet available implicitly to the PetTrainer?

Aucun commentaire:

Enregistrer un commentaire