This is cross-posted from the coursera functional programming course because there's a lot less activity on that forum.
I wrote the following code (parts are redacted because it's homework):
type Occurrences = List[(Char, Int)]
def subtract(x: Occurrences, y: Occurrences): Occurrences = {
val mx: Map[Char, Int] = x toMap
y.foldLeft (redacted) (redacted => simple expression using updated and -)) toList
}
This produces the following compile error:
type mismatch; found : Map[Char,Int] required: <:<[(Char, Int), (?, ?)]
However if I add a copy of the third line, without the toList, in between via a val statement, the error goes away:
type Occurrences = List[(Char, Int)]
def subtract(x: Occurrences, y: Occurrences): Occurrences = {
val mx: Map[Char, Int] = x toMap
val foo: Map[Char, Int] = y.foldLeft (redacted) (redacted => simple expression using updated and -))
y.foldLeft (redacted) (redacted => simple expression using updated and -)) toList
}
I'm guessing this has something to do with giving some kind of extra hint to the type checker, but does anyone know specifically why this happens?