我一定遗漏了一些东西,但我没有看到类中的特性productElement和特性的实现。productArityProductOption
所以两个问题:
- 为什么会
Option延长Product? - 怎么可能
Option(或它的两个子类中的任何一个)没有实现这两种方法?
当您Option在 scala 中生成一个时,您实际上是在生成一个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
*/
我希望这能回答你的两个问题,编译器的魔力!