代码之家  ›  专栏  ›  技术社区  ›  Araeos

Scala:未记录的提取器“::`

  •  0
  • Araeos  · 技术社区  · 8 年前

    使用 api documentation 对于Scala,很明显列表是如何使用 :: 语法,它引用列表上的方法。

    但是中缀符号中的提取器用于模式匹配,如 { case 1 :: 2 :: _ => ??? } 需要具有unapply方法的对象。 所以我的问题是:这个没有文档的提取器在哪里 :: 来自斯卡拉?

    2 回复  |  直到 8 年前
        1
  •  1
  •   Nagarjuna Pamu    8 年前

    以下是如何 :: 实现看起来像

    /** 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?

        2
  •  0
  •   Araeos    8 年前

    假使 :: 在列表中,提取器作为 case class :: . 的用法 :: 在模式中,匹配被称为构造函数模式。

    推荐文章