Designation of the argument hidden inside the method

I have a method that calls several other methods that use the implicit type Foo . I would like to make f hidden inside Foo. Here is how I do it:

 def myMethod(a: Int, f: Foo) = { implicit val implicitF = f; somethingThatNeedsFoo(a) } 

I don't want to mark f implicit in myMethod signature, since Foos are not implicit in the context of using myMethod. My question is: is there a more idiomatic (or compressed) way to achieve this effect than using a temporary variable?

+7
source share
2 answers

You can explicitly pass implicit parameters, so even if there is no Foo in the implicit region of the caller, the caller can simply pass it explicitly.

As commented, you can use the same trick to pass Foo to somethingThatNeedsFoo :

  def myMethod(a: Int, f: Foo) = somethingThatNeedsFoo(a)(f) 
+4
source

I have never seen this actually be used, but you can avoid binding a variable to a name. Using, of course, underscores:

 implicit val _ = f 

Identification is not recommended for this use.

+6
source

All Articles