1

To paraphrase the title question, are implicit parameters to a function implicit values within the function's scope?

Here's a small test:

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()
  }

}

Now run it:

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

This seems to demonstrate to me that implicit parameters are themselves implicit since myOtherFun can find an implicit argument, which I did not believe was the case for some time! I suppose this has advantages and disadvantages, but I'm just here to learn the facts; I looked at http://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html (based on Passing scala.math.Integral as implicit parameter) and didn't see any mention of this fact, if I'm understanding things correctly.

Community
  • 1
  • 1
bbarker
  • 11,636
  • 9
  • 38
  • 62

1 Answers1

3

Yes implict params can be accessed by other functions with matching implicit types within scope

Taken from 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.

Community
  • 1
  • 1
edwardsmatt
  • 2,034
  • 16
  • 18
  • Ah i see "that denote an implicit definition *or an implicit parameter*"; not sure I saw this previously, but even if I did, I probably glossed over it - thanks!. – bbarker Sep 20 '16 at 14:07