Using an explicit value is passed to the implicit parameter as the new implicit value

Consider the case class:

case class IntPrinter(implicit val i: Int) {
  def print()(implicit i: Int) = println(i)
}

I can, for example, explicitly pass the value for the implicit argument as follows:

val p = IntPrinter()(9)

I was told by the IRC that from now on, the explicitly transmitted value will be implicitly passed for printing when called, but this is not so:

p.print()
 error: could not find implicit value for parameter i: Int

Am I doing something wrong or am I misunderstanding / receiving incorrect information? Is there any way to achieve this?


EDIT:, in fact, it works as expected if I import p._as follows:

import p._
p.print()

What really prints 9.

Is this the right behavior? Uses importas a bad idea, how does it sound? How do i solve this?

+3
source share
2 answers

, . implicit , print IntPrinter , () , import .

, :

case class IntPrinter(implicit val i: Int) {
  def print()(implicit i: Int) = println(i)
  def printProxy() = print()
}

p.printProxy , , ( printProxy IntPrinter).

+1

, . ? , :

scala> case class IntPrinter(i: Int) { def print() = println(i) }
defined class IntPrinter

scala> val p = IntPrinter(9)
p: IntPrinter = IntPrinter(9)

scala> p.print()
9

, IntPrinter :

scala> case class IntPrinter(implicit val i: Int) { def print() = println(i) }
defined class IntPrinter

scala> val p = IntPrinter()(9)
p: IntPrinter = IntPrinter(9)

scala> p.print()
9

, , ; , i , - , .

+1

All Articles