Why does currying in Scala need multiple parameter lists?

后端 未结 2 1203
一生所求
一生所求 2021-01-04 19:11

Suppose I have a function of 2 parameters that I need to apply partially, I need to define it as:

def f(a: Int)(b: Int) = { /* some code */ }
2条回答
  •  渐次进展
    2021-01-04 19:54

    A few reasons 'real' currying requires multiple parameter lists in Scala:

    • overloading. Unlike purely functional languages, in Scala you can overload methods. If you partially apply a function, the compiler may not be able to distinguish which overload you mean. The specification limits overloading resolution to the first parameter list.

    • error messages. "not enough arguments for method call" is a very useful (and easy to understand) error message. If one allowed currying for any method, the error message would be "required: but "some function type with many arrows" type.

    • performance. Running on the JVM makes it very efficient to call methods, while functions (going through an interface call) are slower.

提交回复
热议问题