Currency Question

Just wanted to know the wrt currying question

If we defined the curriedNewSum currency function

scala> def curriedNewSum(x : Int)(y : Int) = x + y curriedNewSum: (x: Int)(y: Int)Int scala> curriedNewSum(10)(20) res5: Int = 30 scala> var tenPlus = curriedNewSum(10)_ tenPlus: (Int) => Int = <function1> scala> tenPlus(20) res6: Int = 30 scala> var plusTen = curriedNewSum(_)(20) <console>:6: error: missing parameter type for expanded function ((x$1) => curri edNewSum(x$1)(20)) var plusTen = curriedNewSum(_)(20) ^ 

So why does curriedNewSum (10) _ work and curriedNewSum (_) (10) not?

+4
source share
3 answers

I am not 100% sure what exactly is the problem, but I strongly suspect that this is not what you think.

Try for example

 var plusTen = curriedNewSum(_) 

You will see that it will return Function1[Int, Function1[Int, Int]] . Now try the following:

 var plusTen = (curriedNewSum(_))(10) 

And see how it works! Well, that means:

 var plusTen = ((x: Int) => curriedNewSum(x))(10) 

While the other way translates to:

 var plusTen = (x) => curriedNewSum(x)(10) 

Something about how the function expands captivates the type inference.

+7
source

I'm not quite sure why it is not working. But it works:

curriedNewSum (_: Int) (20)

After I think about it more, there may be a chance of overloaded curriedNewSum methods

 curriedNewSum(x:Double)(y:Int) curriedNewSum(x:Float)(y:Int) 

which will be selected? The type definition explicitly indicates which method you want.

0
source

I suspect that the type should be inferred if there is no ambiguity, and that it is a mistake or intentionally omitted.

0
source

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


All Articles