Understanding Currying in Scala

后端 未结 3 900
夕颜
夕颜 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:30

    Your question interested me so I tried this out my self. They actually desugar down to some very different constructs. Using

    def addCurr(a: String)(b: String): String = {a + " " + b}

    This actually compiles to

    def addCurr(a: String, b: String): String = {a + " " + b}

    So it completely removes any currying effect, making it a regular arity-2 method. Eta expansion is used to allow you to curry it.

    def add(a:String): String => String = {b => a + " " + b}

    This one works as you would expect, compiling to a method that returns a Function1[String,String]

提交回复
热议问题