10

I have scala functions called "run1" and "run2" which accept 3 parameters. When I apply them I want to provide an anonymous function with implicit parameters. It doesn't work in both cases in example codes below. I want to know if

  1. Is that even possible?
  2. If it is possible, what's 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()
          }
        }

4
  • 1
    possible duplicate of Function literal with multiple implicit arguments Commented Jul 18, 2013 at 2:46
  • It doesn't work in my case at "run2", I'm using scala 2.10.0. Commented Jul 18, 2013 at 2:57
  • Your type isn't curried, you're currying it in the run2 function itself. fType = Object => String => Long => Object would work. Commented Jul 18, 2013 at 2:58
  • But run2 doesn't take a curried function, which you're trying to give it. Commented Jul 18, 2013 at 3:01

1 Answer 1

17

You're currying the function inside run2 so run2 still needs a non-curried 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()
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.