I am trying currently trying to solve some Scala problem set for getting to know with the language. I am stuck with problem 11 where my solution will not compile. My question is: Why is this illegal in Scala?
def countPackSingle[A](list: List[A]): List[Any] = {
pack(list) map {ls => {
case head :: Nil => head
case head :: tail => List(tail.size + 1, head)
}}
}
IntelliJ is happy about this definition, but the compiler complains:
missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)Expected type was: ?
pack(list) map {ls => { ^
I do not really get what this message is trying to tell me. Can the scala compiler not infere the type of ls
? When I specify the type by ls: List[A]
, the problem however remains.
At this occasion: Why can I specify the type for the argument ls
when using curly braces { }
but not when using parenthesises ( )
? Until today, I am looking for a good resource explaining the difference in Scala. So far, I believed it only makes a real difference when using the literals to create parial functions via `case and on some other rare occasions.
Thanks for help!