1

I have a scala file which uses a trait to provide implicit values:

class A
class B

class Service  {
  def check(implicit a:A, b:B) = println("hello")
}

object Main extends App with Dependencies {

  def run() {
    val service = new Service
    service.check
  }

}

trait Dependencies {
  implicit val a = new A
  implicit val b = new B
}

But it can't be compiled, the error is:

 error: could not find implicit value for parameter a: A
           service.check
               ^

But, if I put the trait Dependencies to the beginning of the file:

trait Dependencies {
  implicit val a = new A
  implicit val b = new B
}

// other code are here

It compiles!

Why we must put it at the beginning?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Freewind
  • 193,756
  • 157
  • 432
  • 708

0 Answers0