【发布时间】:2014-02-14 05:02:58
【问题描述】:
我一定遗漏了一些东西,但我没有看到 Option 类中 Product 特征的 productElement 和 productArity 的实现。
所以两个问题:
- 为什么
Option会扩展Product? -
Option(或其两个子类中的任何一个)怎么可能没有实现这两种方法?
【问题讨论】:
标签: scala scala-option
我一定遗漏了一些东西,但我没有看到 Option 类中 Product 特征的 productElement 和 productArity 的实现。
所以两个问题:
Option 会扩展Product?Option(或其两个子类中的任何一个)怎么可能没有实现这两种方法?【问题讨论】:
标签: scala scala-option
当您在 Scala 中生成 Option 时,您实际上是在生成 Some 或 None,它们都是案例类/对象。 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
*/
我希望能回答你的两个问题,编译器魔法!
【讨论】: