Why can't scala find the implicit here?
class A
class Foo {
lazy val x = implicitly[A]
implicit lazy val a = new A
}
error: could not find implicit value for parameter e: A
But this works just fine:
class Foo {
lazy val x = implicitly[A]
implicit lazy val a: A = new A // note explicit result type
}
defined class Foo
FWIW I'm stuck on Scala 2.10 for this application. Also, replacing lazy val
with def
doesn't seem to change anything.
In my actual application, I've got a file with a bunch of implicits defined for various domain objects, some of which depend on each other. It seems like a nightmare to try and arrange them in a way that makes sure all dependencies come before their respective dependents, so I marked them all as lazy
. Having to explicitly declare the type of each of these vals muddies up the code, and seems like it should be unnecessary. Any way around this?
implicit lazy val a = new A; lazy val x = implicitly[A]
. Shouldn't this be a bug in Scala ? – Bathsheeb