我想在Scala中編譯簡單的helloworld, 並獲得錯誤「scala:value capitalize is not char of member」 爲什麼編譯器認爲newW是Char?Scala:映射字符串時出現錯誤的類型推斷?
val dict = Map(
"hello" -> "olleh",
"world" -> "dlrow"
)
def translate(input: String): String = {
input.split("""\s+""").map(w => dict.getOrElse(w.toLowerCase, w).map(newW =>
(if (w(0).isUpper) newW.capitalize else newW))
).mkString(" ")
}
從技術上講,不像Haskell,'String'不是'List [Char]'。我認爲他們之間有一個隱含的轉換。 – 2013-02-14 23:53:20
你說得對,我已經相應地更新了文字。 – 2013-02-14 23:55:46