Macwire, wireWith and implicit parameters

1.5k Views Asked by At

wireWith seems to have some issues with resolving implicit parameters.

Minimal example:

import com.softwaremill.macwire._

object A {
  def props(x: Int)(implicit y: String): A = new A(x)
}

class A(x: Int)(implicit y: String) {
   val sum: String = s"$x + $y"
}

object App {
   def main(): Unit = {
     val xVal: Int = 3
     implicit val yVal: String = "5"

     // val aInstance = wire[A] // works
     // val aInstance = A.props(xVal) // works
     val aInstance: A = wireWith(A.props _) // compile error

     println(aInstance.sum)
  }
}

App.main()

Error:

Error:(21, 33) type mismatch; found   : Int required: String
    val aInstance: A = wireWith(A.props _)
                           ^

If implicit is ommitted at yVal, it complains about missing implicit:

Error:(18, 36) could not find implicit value for parameter y: String 
    val aInstance: A = wireWith(A.props _) // compile error
                              ^

This is a simplified example though - in my production code i try to wire Actor props:

object Actor {
    def props
        (dependency: Dependency, refreshInterval: FiniteDuration @@ CacheRefreshInterval)
        (implicit ec: ExecutionContext): Props =
            Props(new Actor(dependency, refreshInterval))
}

// DI container
protected implicit def executionContext: ExecutionContext
protected lazy val dependency: Dependency = wire[Dependency]
protected lazy val refreshInterval = 2.second.taggedWith[CacheRefreshInterval]
protected lazy val actorProps: Props @@ ActorProps = actorwireWith(Actor.props _)

and get different compile error:

too many arguments for method props: (implicit ec: scala.concurrent.ExecutionContext)akka.actor.Props

I've tried making implicit parameter explicit and it worked just fine, except it's a bit against usual practice of passing execution context implicitly.

Am I doing something wrong or is it an issue in macwire?

Issue in Github: https://github.com/adamw/macwire/issues/125

1

There are 1 best solutions below

0
Frederic A. On

If you were using wireWith(A.props _) as a workaround for macwire's inability to wire actors when their constructor are using implicit parameters (see macwire issue 121), you could switch to macwire 2.3.1 and wire your actors using wireActor[A], wireAnonymousActor[A] or wireProps[A]. issue 121 was fixed in macwire 2.3.1.