首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scala:未归档的Extractor `::`

Scala:未归档的Extractor `::`
EN

Stack Overflow用户
提问于 2017-12-20 17:40:00
回答 2查看 64关注 0票数 0

通过使用Scala的api documentation,很容易看出如何使用::语法构造列表,该语法指的是列表上的方法。

但是中缀表示法中用于模式匹配的提取器,就像在{ case 1 :: 2 :: _ => ??? }中一样,需要一个带有unapply方法的对象。所以我的问题是:这个没有文档记录的提取器::在Scala中是从哪里来的?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-20 17:49:21

下面是::实现的样子

代码语言:javascript
复制
/** 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?

票数 1
EN

Stack Overflow用户

发布于 2017-12-20 17:49:39

对于列表上的::,提取器是作为case class ::的一部分生成的。在模式匹配中使用::称为构造器模式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47902928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档