通过使用Scala的api documentation,很容易看出如何使用::语法构造列表,该语法指的是列表上的方法。
但是中缀表示法中用于模式匹配的提取器,就像在{ case 1 :: 2 :: _ => ??? }中一样,需要一个带有unapply方法的对象。所以我的问题是:这个没有文档记录的提取器::在Scala中是从哪里来的?
发布于 2017-12-20 17:49:21
下面是::实现的样子
/** A non empty list characterized by a head and a tail.
* @param head the first element of the list
* @param tl the list containing the remaining elements of this list after the first one.
* @tparam B the type of the list elements.
* @author Martin Odersky
* @version 1.0, 15/07/2003
* @since 2.8
*/
@SerialVersionUID(509929039250432923L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
override def tail : List[B] = tl
override def isEmpty: Boolean = false
}欲了解更多信息,请访问Scala's '::' operator, how does it work?
发布于 2017-12-20 17:49:39
对于列表上的::,提取器是作为case class ::的一部分生成的。在模式匹配中使用::称为构造器模式。
https://stackoverflow.com/questions/47902928
复制相似问题