Head :: tail matching for strings
1 answer
case h +: t
means case +:(h, t)
. There is a +:
object using the unapply
method.
unapply
method +:
is defined only for SeqLike
and String
not SeqLike
.
You need a custom unapply
method like this:
object s_+: { def unapply(s: String): Option[(Char, String)] = s.headOption.map{ (_, s.tail) } } "abc" match { case h s_+: t => Some((h, t)) case _ => None } // Option[(Char, String)] = Some((a,bc))
+7