gpt4 book ai didi

scala - 为什么 Option 会扩展 Product?

转载 作者:行者123 更新时间:2023-12-01 10:01:38 26 4
gpt4 key购买 nike

我一定是遗漏了什么,但我没有看到 ProductproductElementproductArity 的实现。 Option 中的特征类。

所以两个问题:

  1. 为什么 Option 会扩展 Product
  2. 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
*/

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

关于scala - 为什么 Option 会扩展 Product?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15373079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com