【问题标题】:Why does Option extend Product?为什么 Option 扩展 Product?
【发布时间】:2014-02-14 05:02:58
【问题描述】:

我一定遗漏了一些东西,但我没有看到 Option 类中 Product 特征的 productElementproductArity 的实现。

所以两个问题:

  1. 为什么Option 会扩展Product
  2. Option(或其两个子类中的任何一个)怎么可能没有实现这两种方法?

【问题讨论】:

    标签: scala scala-option


    【解决方案1】:

    当您在 Scala 中生成 Option 时,您实际上是在生成 SomeNone,它们都是案例类/对象。 scala 编译器对案例类发挥了神奇的作用,并为它们生成 Product 方法。

    来自 Scala 2.10 Product.scala:

    /** Base trait for all products, which in the standard library include at
     *  least [[scala.Product1]] through [[scala.Product22]] and therefore also
     *  their subclasses [[scala.Tuple1]] through [[scala.Tuple22]].  In addition,
     *  all case classes implement `Product` with synthetically generated methods.
     *
     *  @author  Burak Emir
     *  @version 1.0
     *  @since   2.3
     */
    

    我希望能回答你的两个问题,编译器魔法!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      相关资源
      最近更新 更多