【问题标题】:Sum or Product Type?总和还是产品类型?
【发布时间】:2015-06-10 15:57:19
【问题描述】:

鉴于以下Algebraic Data Type

scala> sealed trait Person
defined trait Person

scala> case class Boy(name: String, age: Int, x: String) extends Person
defined class Boy

scala> case class Girl(name: String, age: Int, y: Boolean) extends Person
defined class Girl

注意 - 我知道它不是递归类型 - 不涉及递归。

那么,这是Sum 还是Product Type?为什么?

【问题讨论】:

  • 两者——这是产品的总和。 Wikipedia entry on ADTs 可能是一个很好的起点。
  • 另请注意,它可能是递归的,但仍然是乘积的总和。
  • @TravisBrown:您应该将其发布为答案。
  • @TravisBrown - case objectcase class 与单个参数组成 Scala 中的 Sum Type
  • @KevinMeredith 您可以将 sum 视为“或”——在此模型中,PersonBoy Girl。 "and" 是产品——Boy 数据类型由名称年龄字符串x组成。

标签: scala algebraic-data-types


【解决方案1】:

在这种情况下,Person 可以被视为 sum 类型,因为它的实例是 BoyGirl

Boy(或Girl)是一种产品类型,因为Boy 的实例是StringIntString 类型的组合。

这是一篇关于这个“混合”案例的非常好的文章Algebraic Data Types in Scala

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    相关资源
    最近更新 更多