隐式参数值本身就是隐式值吗?

Are implicit parameter values implicit values themselves?

换句话说,函数的隐式参数是函数范围内的隐式值吗?

这里有一个小测试:

object ImplicitTest{

  case class Foo()

  def myOtherFun()(implicit myfoo: Foo) = {
    val grabImpFoo = implicitly[Foo]
    println(myfoo.toString + " from myOtherFun")
  }
}

import ImplicitTest._

class ImplicitTest {
  def myFun()(implicit myfoo: Foo) = {
    println(myfoo.toString)
    myOtherFun()
  }

}

现在运行它:

  implicit val foo = Foo()
  val it = new ImplicitTest()
  it.myFun()

这似乎向我证明了隐式参数本身是隐式的,因为 myOtherFun 可以找到一个隐式参数,有一段时间我不相信是这样!我想这有利有弊,但我只是来这里了解事实;我查看了 http://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html (based on Passing scala.math.Integral as implicit parameter) 并且没有看到任何提及这个事实,如果我理解正确的话。

Years 隐式参数可以被范围内具有匹配隐式类型的其他函数访问

取自http://docs.scala-lang.org/tutorials/tour/implicit-parameters.html

The actual arguments that are eligible to be passed to an implicit parameter fall into two categories:

  • First, eligible are all identifiers x that can be accessed at the point of the method call without a prefix and that denote an implicit definition or an implicit parameter.

  • Second, eligible are also all members of companion modules of the implicit parameter’s type that are labeled implicit.