【发布时间】:2014-09-20 23:33:02
【问题描述】:
如何在 Scala 中对字符串进行模式匹配:
scala> "55" match {
| case x :: _ => x
| }
<console>:9: error: constructor cannot be instantiated to expected type;
found : scala.collection.immutable.::[B]
required: String
case x :: _ => x
^
在 Haskell 中,String 是一个字符列表 [Char]:
Prelude> :i String
type String = [Char] -- Defined in `GHC.Base'
所以它支持String上的模式匹配。
如何在 Scala 中做到这一点?
【问题讨论】:
-
我打算添加一个答案,但那个重复的问题很好地涵盖了它
-
感谢您指出这个骗局。我的错误(但我很高兴我从即席的回答中学到了)