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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.