[英]Scala: What is :: in { case x :: y :: _ => y}? Method or case class?
object Test1 extends App {
val list: List[Int] => Int = {
case x :: y :: _ => y //what is ::? method or case class?
}
println(list(List(1, 2, 3))) //result is 2.
}
I set "syntax coloring" in scala IDE, foreground color of method I set Red.我在 Scala IDE 中设置了“语法着色”,方法的前景颜色我设置为红色。 code snapshot:
代码截图:
And I can't open declaration of black:: , so I don't know what it is.
而且我无法打开black::的声明,所以我不知道它是什么。
If black:: is method, it should be called by this way:如果black::是方法,应该这样调用:
... {case _.::(y).::(x) => y} //compile failed!
So, What is black:: ?那么,什么是黑色:: ? method or case class?
方法还是案例类?
Thanks a lot!非常感谢!
I think it's a method as described here .我认为这是此处描述的方法。 For history's sake, in case that page goes away, here's the blurb:
为了历史的缘故,万一该页面消失了,这里是简介:
About pattern matching on Lists
关于列表上的模式匹配
If you review the possible forms of patterns explained in Chapter 15, you might find that neither List(...) nor:: looks like it fits one of the kinds of patterns defined there.
如果您查看第 15 章中解释的可能的模式形式,您可能会发现 List(...) 和 :: 看起来都不适合其中定义的一种模式。 In fact, List(...) is an instance of a library-defined extractor pattern.
事实上,List(...) 是库定义的提取器模式的一个实例。 Such patterns will be treated in Chapter 24. The "cons" pattern x:: xs is a special case of an infix operation pattern.
此类模式将在第 24 章中处理。“cons”模式 x:: xs 是中缀操作模式的特例。 You know already that, when seen as an expression, an infix operation is equivalent to a method call.
您已经知道,当被视为表达式时,中缀操作等同于方法调用。 For patterns, the rules are different: When seen as a pattern, an infix operation such as p op q is equivalent to op(p, q).
对于模式,规则是不同的:当被视为模式时,中缀运算(例如 p op q)等同于 op(p, q)。 That is, the infix operator op is treated as a constructor pattern.
也就是说,中缀运算符 op 被视为构造函数模式。 In particular, a cons pattern such as x:: xs is treated as::(x, xs).
特别是,像 x:: xs 这样的 cons 模式被视为 ::(x, xs)。 This hints that there should be a class named:: that corresponds to the pattern constructor.
这暗示应该有一个名为 :: 的类对应于模式构造函数。 Indeed there is such as class.
确实有这样的类。 It is named scala.:: and is exactly the class that builds non-empty lists.
它被命名为 scala.:: 并且正是构建非空列表的类。 So:: exists twice in Scala, once as a name of a class in package scala, and again as a method in class List.
So:: 在 Scala 中存在两次,一次作为 scala 包中的类名,另一次作为类 List 中的方法。 The effect of the method:: is to produce an instance of the class scala.::.
method:: 的作用是生成类 scala.:: 的一个实例。 You'll find out more details about how the List class is implemented in Chapter 22.
您将在第 22 章中找到有关如何实现 List 类的更多详细信息。
So it's scala.::(a,b)所以它是 scala.::(a,b)
Here Sequence pattern is applied.这里应用了序列模式。
means, the passed sequence first value is mapped to x and second value is mapped to y and all remaining values are applied with wildcard pattern (_).意味着,传递的序列第一个值映射到 x,第二个值映射到 y,所有剩余值都应用通配符模式 (_)。
Here finally the case returns y means second value.最后,案例返回 y 表示第二个值。
this case returns third element from the sequence.本例返回序列中的第三个元素。
in this example first and second element are used underscore as we don't need THESE elements hence replaced with _.在此示例中,第一个和第二个元素使用下划线,因为我们不需要这些元素,因此替换为 _。
Also it throws Exception if the passing list size is less than what it expects, for Example the below throws exception:如果传递的列表大小小于预期,它也会抛出异常,例如下面抛出异常:
val third: List[Int] => Int = {case _ :: _ :: z :: _ => z}
third(List(1, 2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.