Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currying function from an existing answer is invalid

Tags:

scala

I tried running the below code:

val f: (a: Int) => (b: Int) => (c: Int) = a + b + c

found in this thread in the REPL and IntellijIDEA but it's apparently invalid.

From the REPL:

scala> val f: (a: Int) => (b: Int) => (c: Int) = a + b + c
<console>:1: error: ')' expected but ':' found.
       val f: (a: Int) => (b: Int) => (c: Int) = a + b + c
                ^

Anyone knows why? My scala version is 2.10.1

Thank you

like image 346
ccheneson Avatar asked Sep 01 '26 17:09

ccheneson


1 Answers

You write the type as if you were writing:

val a: 5 = 5

What you want is more like

val f  = (a: Int) => (b: Int) => (c: Int) => a+b+c

To elaborate further the REPL will write

f: Int => (Int => (Int => Int)) = <function1>

Because function definition is right associative you could the type of f explicitly as follows

f: Int => Int => Int => Int = (a: Int) => (b: Int) => (c: Int) => a+b+c

If you explicitly give the function type like this the compiler does not need information about what a,b, and c are and you could simply write a => b => c => a+b+c instead.

like image 195
uberwach Avatar answered Sep 04 '26 19:09

uberwach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!