wireWith似乎在解析隐式参数方面有一些问题。
最小示例:
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:(21, 33) type mismatch; found : Int required: String
val aInstance: A = wireWith(A.props _)
^如果在yVal中省略了implicit,它会抱怨缺少implicit:
Error:(18, 36) could not find implicit value for parameter y: String
val aInstance: A = wireWith(A.props _) // compile error
^这是一个简化的例子--在我的产品代码中,我尝试连接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 _)并得到不同的编译错误:
too many arguments for method props: (implicit ec: scala.concurrent.ExecutionContext)akka.actor.Props我尝试过显式地设置隐式参数,它工作得很好,除了这与隐式传递执行上下文的通常做法有点不同。
是我做错了什么,还是在macwire中有问题?
Github中的问题:https://github.com/adamw/macwire/issues/125
https://stackoverflow.com/questions/48657546
复制相似问题