Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to implicitly convert an implicit parameter in Scala?
    text
    copied!<p>Is there a way to make this work (Scala 2.8.1):</p> <pre><code>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 </code></pre> <p>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:</p> <pre><code>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) {} } </code></pre> <p>Where <code>UserStore</code> is:</p> <pre><code>class UserStore(implicit db: DbAccess) </code></pre> <p>Am i doing something wrong?</p> <p><strong>UPDATE</strong></p> <p>Thanks to <em>Easy Angel</em> 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'.</p> <p>Can it be solved?</p> <p><strong>UPDATE2</strong></p> <p>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:</p> <pre><code>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 </code></pre> <p>Any hints?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload