How do you create an anonymous scala function with several implicit parameters

I have scala functions called "run1" and "run2" that take 3 parameters. When I apply them, I want to provide an anonymous function with implicit parameters. It does not work in both cases in the examples below. I want to know if

  • Is it possible?
  • If possible, what is the syntax?
object Main extends App { type fType = (Object, String, Long) => Object def run1( f: fType ) { f( new Object, "Second Param", 3) } run1 { implicit (p1, p2, p3) => // fails println(p1) println(p2) println(p3) new Object() } def run2( f: fType ) { val fC = f.curried fC(new Object)("Second Param")(3) } run2 { implicit p1 => implicit p2 => implicit p3 => // fails println(p1) println(p2) println(p3) new Object() } } 
+8
scala
source share
1 answer

You are looking at a function inside run2 , so run2 still needs a non-curry function. See the code below for a version that works:

 object Main extends App { type fType = (Object, String, Long) => Object type fType2 = Object => String => Long => Object //curried def run1( f: fType ) { f( new Object, "Second Param", 3) } // Won't work, language spec doesn't allow it run1 { implicit (p1, p2, p3) => println(p1) println(p2) println(p3) new Object() } def run2( f: fType2 ) { f(new Object)("Second Param")(3) } run2 { implicit p1 => implicit p2 => implicit p3 => println(p1) println(p2) println(p3) new Object() } } 
+15
source share

All Articles