Scala currying and output type

I am trying to figure out how to use currying in Scala.

In the following snippet:

object Foo {

  def foo(a:Int)(b:Int)(c:Int) : Int = a + b + c

  def main(a:Array[String]) {

    val f = foo(1)(2) _ 
    def g : Int => Int = foo(1)(2) 
    // def h = foo(1)(2) 

    println(f(3))
    println(g(3))
    // println(h(3))
  }
}

definitions are used for fand g, for hdoes not exist:

/home/vlad/Desktop/b.scala:9: error: missing arguments for method foo in object Main;
follow this method with `_' if you want to treat it as a partially applied function
    def h = foo(1)(2)

What are the reasons for this illegal? It seems to me that Scala should be able to figure out that after the call foo(1)(2)you will be left with Int => Int.

+4
source share
1 answer

It is intended. If it were allowed (in some languages ​​it is really allowed), it would make it work when you forgot to specify an argument, and instead of a compilation error, you expected. This happens so often that scala authors decide a compromise here and prohibit this designation.

, . " ?" http://www.artima.com/pins1ed/functions-and-closures.html#8.7
+9

Source: https://habr.com/ru/post/1616730/


All Articles