Pattern matching a String as Seq[Char]

前端 未结 3 466
南方客
南方客 2020-12-06 05:58

In Scala it is possible formulate patterns based on the invididual characters of a string by treating it as a Seq[Char].

An example of this feature is mentioned in

3条回答
  •  广开言路
    2020-12-06 06:28

    I'm going to echo everything that andri said. For interoperability, Scala strings are java.lang.Strings. In Predef, there's an implicit conversion from String to RichString, which implements Seq[Char].

    A perhaps nicer way of coding the pattern match, without needing an intermediate val z to hold the Seq[Char]:

    def containsScala(x: String): Boolean = {
      (x: Seq[Char]) match {
        ...
      }
    }
    

提交回复
热议问题