Is there a way to make this work (Scala 2.8.1):
class A
class B
def f(implicit b: B) {}
implicit val a = new A
implicit def aToB(a: A) = new B
f(a) // works ok
f // error: could not find implicit value for parameter b: B
Actually my problem is with Lift’s (2.2) dependency injection, i’m trying to convert Vendor[T] to T and implicitly require it in a class constructor without adding imports after each val:
object DependencyFactory extends Factory {
implicit def vendorToVal[T](vendor: Vendor[T]): T = vendor.vend
implicit val db = new FactoryMaker[DbAccess](Model) {}
//uncommenting the following line makes it work, but can we avoid it?
//import db._
implicit val userStore = new FactoryMaker[UserStore](new UserStore) {}
}
Where UserStore is:
class UserStore(implicit db: DbAccess)
Am i doing something wrong?
UPDATE
Thanks to Easy Angel for answering the first part. But it doesn’t solve my Lift DI problem because it turns out that there is an opposite conversion in scope (from T to Vendor[T]) and having those both leads to ‘error: diverging implicit expansion’.
Can it be solved?
UPDATE2
Wow one more problem after previous: having a conversion from some Container[T] to T with implicit instance of Container[U] in scope and a function with implicit parameter U leads to ‘diverging implicit expansion’ too:
class A
case class Vendor[T](v: T)
def f(implicit a: A) {}
implicit val vendor = Vendor(new A)
implicit def vendorToVal[T](implicit v: Vendor[T]) = v.v
f
Any hints?
You almost made it. You only need to declare
aimplicit:In this case compiler tries to find some implicit
Bfor the first implicit argument offand it findsaToB. Than compiler ties to satisfyaToB‘s requirement (implicit a: A) and finds yourimplicit val a.