Trần Tú Nam
Trần Tú Nam

Reputation: 29

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

how to fix problem but dont use this solution

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

Upvotes: 2

Views: 3234

Answers (1)

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

One way is to Explicitly passing b

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

Second way is to Mark b as implicit in the signature of p2

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

Upvotes: 5

Related Questions