3

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

Community
  • 1
  • 1
ccheneson
  • 49,072
  • 8
  • 63
  • 68

1 Answers1

3

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.

uberwach
  • 1,119
  • 7
  • 14