简体   繁体   English

Macwire,wireWith和隐式参数

[英]Macwire, wireWith and implicit parameters

wireWith seems to have some issues with resolving implicit parameters. wireWith似乎在解析隐式参数方面存在一些问题。

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: 如果yVal yVal 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: 不过,这是一个简化的示例-在我的生产代码中,我尝试连接Actor道具:

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? 我是在做错什么,还是在Macwire中有问题?

Issue in Github: https://github.com/adamw/macwire/issues/125 在Github中发布: https : //github.com/adamw/macwire/issues/125

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] . 如果您使用wireWith(A.props _)作为解决方案,以解决macwire在构造函数使用隐式参数时无法连接actor的问题 (请参阅macwire 问题121 ),则可以切换到macwire 2.3.1并使用wireActor[A]wireAnonymousActor[A]wireProps[A] issue 121 was fixed in macwire 2.3.1. 问题121在Macwire 2.3.1中已修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM