简体   繁体   English

Scala中的String和Int模式匹配

[英]Pattern matching in Scala for String and Int

Why does pattern matching in Scala work for String and AnyVal s such as Int ? 为什么Scala中的模式匹配可用于StringAnyVal例如Int

Usually we see things like Case classes or Extractors ... 通常我们会看到诸如Case classesExtractors Case classes东西。

Extractors and case classes are used just for two of 13 kinds of patterns in Scala , "Extractor patterns" and "Constructor patterns" respectively. 提取器和案例类仅用于Scala13种模式中的两种 ,分别为“提取器模式”和“构造器模式”。 You can't use Int or String in this kind of pattern ( case String(x) ). 不能在这种模式( case String(x) )中使用IntString But you can use them in other kinds: 但是您可以将它们用于其他类型:

  1. Typed patterns, as in case x: String . 键入模式,例如case x: String In that case there is nothing special about String , you can do the same with any class (but there is something special about Int and other primitives: case x: Int actually checks if the received object is a java.lang.Integer in most cases). 在那种情况下, String没有什么特别的,您可以对任何类都做同样的事情(但是Int和其他原语有一些特别的事情: case x: Int在大多数情况下, case x: Int实际上检查接收到的对象是否是java.lang.Integer 。 )。

  2. Literal patterns, as in case 0 or case "" . 文字模式,如case 0case "" Again, nothing special about strings, this works for all literals. 同样,字符串没有什么特别的,这适用于所有文字。

java.lang.String is enriched with scala.collection.immutable.StringOps ( http://www.scala-lang.org/api/2.11.8/#scala.collection.immutable.StringOps ) which mix scala.collection.immutable.StringLike ( http://www.scala-lang.org/api/2.11.8/#scala.collection.immutable.StringLike ) in. There you can find complementary methods, like apply. java.lang.String富含scala.collection.immutable.StringOpshttp://www.scala-lang.org/api/2.11.8/#scala.collection.immutable.StringOps ),其中混合了scala.collection.immutable.StringLikehttp://www.scala-lang.org/api/2.11.8/#scala.collection.immutable.StringLike )中。在那里您可以找到补充方法,例如apply。

String is a bit special as well, you can convert it to list of Char s, and use List extractors then like case List(a,b) or case x:xs on a String , bearing in mind that a and b will be Char s; String也有点特殊,您可以将其转换为Char的列表,然后使用List提取器,如String上的case List(a,b)case x:xs ,请记住ab将为Char S; x: Char and xs: List[Char] x: Charxs: List[Char]

All primitive types have Rich* classes (eg scala.runtime.RichBoolean , scala.runtime.RichByte ). 所有原始类型都具有Rich *类(例如scala.runtime.RichBooleanscala.runtime.RichByte )。

Value classes mechanics is used to enrich all of the above mentioned types ( http://docs.scala-lang.org/overviews/core/value-classes.html ). 值类机制用于丰富上述所有类型( http://docs.scala-lang.org/overviews/core/value-classes.html )。 In compile time they have a wrapper type, like RichBoolean or RichInt but in runtime they are pure Boolean or Int types. 在编译时,它们具有包装器类型,例如RichBooleanRichInt但在运行时,它们是纯布尔或Int类型。 In such way avoiding overhead of creating runtime objects. 这样,避免了创建运行时对象的开销。

val x: Any = 5
def f[T](v: T) = v match {
  case _: Int    => "Int"
  case _: String => "String"
  case _         => "Unknown"
}

You don't have to define unapply in class to able to use that class in switch/case -style pattern matching. 你不必定义unapply在课堂上能够使用类switch/case式的模式匹配。 unapply is used to deconstruct object, so, if you want to match switch in List -style ( case x:xs ), you should use unapply/unapplySeq . unapply用于解构对象,因此,如果要以List样式( case x:xs )匹配switch,则应使用unapply/unapplySeq Good example here are regexps, are they are constructed from strings -- "something".r (note .r in the end). regexp是一个很好的例子,它们是由字符串构造的- "something".r (最后请注意.r )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM