Understanding Currying in Scala

后端 未结 3 901
夕颜
夕颜 2021-01-24 04:45

I\'m getting problems to understand the currying concept, or at least the SCALA currying notation.

wikipedia says that currying is the technique of translating the evalu

3条回答
  •  无人及你
    2021-01-24 05:17

    Curried methods are syntactic sugar, you were right about this part. But this syntactic sugar is a bit different. Consider following example:

    def addCur(a: String)(b: String): String = { a + b }
    
    def add(a: String): String => String = { b => a + b }
    
    val functionFirst: String => String = add("34")
    val functionFirst2 = add("34")_
    val functionSecond: String => String = add("34")
    

    Generaly speaking curried methods allows for partial application and are necessary for the scala implicits mechanism to work. In the example above i provided examples of usage, as you can see in the second one we have to use underscore sign to allow compiler to do the "trick". If it was not present you would receive error similar to the following one:

    Error:(75, 19) missing argument list for method curried in object XXX Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing curried_ or curried(_)(_) instead of curried.

提交回复
热议问题