Why doesn’t this work in scala:
val cloz: (Int,String => String) = (num: Int, str: String) => {
str+"-"+num
}
I see a few examples of closures being defined with only 1 arg, like this:
val thingy: (Int => Int) = (num: Int) => {
num * 2
}
But absolutely nowhere (including Scala ebooks) could I find any information explaining the syntax of “val” closures.
Thanks!
Jamie
The correct syntax is:
By the way, in this simple case you can also simplify expression like this (especially if you already explicitly specifying the type of the function):
Update
You can also use REPL to explore Scala – it’s very nice tool. You can start it just by starting
scalawithout any arguments. Here is example session:As you can see it not only allows you to interactively execute code, but also prints type information if you define something.