首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Macwire、wireWith和隐式参数

Macwire、wireWith和隐式参数
EN

Stack Overflow用户
提问于 2018-02-07 14:44:10
回答 1查看 1.1K关注 0票数 0

wireWith似乎在解析隐式参数方面有一些问题。

最小示例:

代码语言:javascript
复制
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()

错误:

代码语言:javascript
复制
Error:(21, 33) type mismatch; found   : Int required: String
    val aInstance: A = wireWith(A.props _)
                           ^

如果在yVal中省略了implicit,它会抱怨缺少implicit:

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

这是一个简化的例子--在我的产品代码中,我尝试连接Actor道具:

代码语言:javascript
复制
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 _)

并得到不同的编译错误:

代码语言:javascript
复制
too many arguments for method props: (implicit ec: scala.concurrent.ExecutionContext)akka.actor.Props

我尝试过显式地设置隐式参数,它工作得很好,除了这与隐式传递执行上下文的通常做法有点不同。

是我做错了什么,还是在macwire中有问题?

Github中的问题:https://github.com/adamw/macwire/issues/125

EN

回答 1

Stack Overflow用户

发布于 2018-03-11 22:41:16

当macwire的构造函数使用隐式参数(请参见macwire issue 121)时,如果您使用wireWith(A.props _)作为无法连接actors的解决方法,则可以切换到macwire 2.3.1,并使用wireActor[A]wireAnonymousActor[A]wireProps[A]连接actors。issue 121已在macwire 2.3.1中修复。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48657546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档