简体   繁体   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

error: could not find implicit value for parameter b: Int 错误:找不到参数b的隐式值:Int

how to fix problem but dont use this solution 如何解决问题,但不要使用此解决方案

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

One way is to Explicitly passing b 一种方法是显式传递b

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

Second way is to Mark b as implicit in the signature of p2 第二种方法是将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