1

I have a case class:

case Context(tracker: Tracker)

I have a processor class with a get def, that expects an implicit parameter of Tracker defined as:

class Processor(){
    def get(id: String)(implicit tracker: Tracker): Unit
}

and I have my calling class:

class repo(process: Processor){
    def get(id: String)(implicit ctx : Context): Unit{
        process.get(id)
    }
}

Is there an easy way for my to map from context -> Tracker? I've been trying to use an implicit def in the companion of 'repo' but am still seeing 'no implicit val available' for the Tracker when calling process.get(id)

athomassi
  • 13
  • 2

1 Answers1

2

You can define implicit in the scope

implicit def ifContextThenTracker(implicit c: Context): Tracker = c.tracker

I've been trying to use an implicit def in the companion of 'repo' but am still seeing 'no implicit val available' for the Tracker when calling process.get(id)

Please see rules of implicit resolution in Where does Scala look for implicits?

You can put ifContextThenTracker into companion object of Tracker if you have access to it. Otherwise you can import implicit.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • The problem is that it doesnt make c.tracker implicit within the context, and I wanted to avoid explicit calls as I like how clean implicits are. Edit: Just saw your edit, checking now – athomassi Sep 20 '21 at 22:16
  • @athomassi Please see update. Does this answer your question? – Dmytro Mitin Sep 20 '21 at 22:18
  • 1
    I added the implicit def to the context, and then imported the context and that worked. Much appreciated! =) – athomassi Sep 20 '21 at 22:22
  • 1
    @athomassi If you put `ifContextThenTracker` into the companion of `Tracker` you don't have to import implicit, otherwise (if you put it into the companion of `Context`, companion of `repo` etc.) you'll have to. – Dmytro Mitin Sep 20 '21 at 22:24