簡體   English   中英

Macwire,wireWith和隱式參數

[英]Macwire, wireWith and implicit parameters

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 yVal 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

如果您使用wireWith(A.props _)作為解決方案,以解決macwire在構造函數使用隱式參數時無法連接actor的問題 (請參閱macwire 問題121 ),則可以切換到macwire 2.3.1並使用wireActor[A]wireAnonymousActor[A]wireProps[A] 問題121在Macwire 2.3.1中已修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM