簡體   English   中英

在Scala中找不到參數的隱式值

[英]Could not find implicit value for parameter in scala

    def p1(c: Int)(implicit b: Int): Unit = {
        println(c + b)
    }

    def p2(a: Int, b: Int): Unit ={
        p1(a)
    }

    p2(5, 6) //result = 11

錯誤:找不到參數b的隱式值:Int

如何解決問題,但不要使用此解決方案

 def p2(a: Int, b: Int): Unit ={
        implicit val bb = b
        p1(a)
    }

一種方法是顯式傳遞b

def p2(a: Int, b: Int): Unit ={
    p1(a)(b)
}

第二種方法是將b標記為隱含在p2的簽名中

def p2(a: Int)(implicit b: Int): Unit ={
  p1(a)
}

暫無
暫無

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

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