Scala 枚举的使用和探索(译)

2019-08-21 14:37:33 浏览数 (1)

Scala 枚举示例和特性

枚举(Enumerations)是一种语言特性,对于建模有限的实体集来说特别有用。一个经典的例子是将工作日建模为一个枚举:每个七天都有一个值。Scala和许多其他语言一样,提供了一种表示枚举的方法:

代码语言:javascript复制
object Weekday extends Enumeration {
  val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
}

现在我们可以准确清晰地表示工作日,而无需使用StringInt等基本类型。Scala枚举还提供了一组有用的特性:

  • 序列化和反序列化方法(Serialize and Deserialize methods),这些方法也会抛出异常:(:
代码语言:javascript复制
scala> Weekday.Monday.toString
res0: String = Monday

scala> Weekday.withName("Monday")
res1: Weekday.Value = Monday

scala> Weekday.withName("Mondai")
java.util.NoSuchElementException: No value found for 'Mondai'
  at scala.Enumeration.withName(Enumeration.scala:124)
  ... 32 elided
  • 提供可读性(human-readable value)的值:
代码语言:javascript复制
object Weekday extends Enumeration {
    val Monday = Value("Mo.")
    val Tuesday = Value("Tu.")
    val Wednesday = Value("We.")
    val Thursday = Value("Th.")
    val Friday = Value("Fr.")
    val Saturday = Value("Sa.")
    val Sunday = Value("Su.")
  }

scala> Weekday.Monday.toString
res0: String = Mo.
  • 列出所有可能的值:
代码语言:javascript复制
scala> Weekday.values
res0: Weekday.ValueSet = Weekday.ValueSet(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
  • 排序(Ordering)。默认情况下,枚举值是按照声明的顺序排序的,排序顺序可以通过覆盖(overridden)原来枚举值的方式改变:
代码语言:javascript复制
object Weekday extends Enumeration {
  val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
}

// 按照枚举值声明的顺序排序
scala> Weekday.values.toList.sorted
res0: List[Weekday.Value] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
代码语言:javascript复制
object Weekday extends Enumeration {
  val Monday = Value(1)
  val Tuesday = Value(2)
  val Wednesday = Value(3)
  val Thursday = Value(4)
  val Friday = Value(5)
  val Saturday = Value(6)
  val Sunday = Value(0)
}

// 按照枚举对应的数字值排序
scala> Weekday.values.toList.sorted
res1: List[Weekday.Value] = List(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

scala.Enumeration的问题

然而,这种方法有一些问题。主要有两个缺点:

  • 擦除(erasure)后枚举具有相同的类型:
代码语言:javascript复制
object Weekday extends Enumeration {
    val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
}

object OtherEnum extends Enumeration {
	val A, B, C = Value
}

def test(enum: Weekday.Value) = {
    println(s"enum: $enum")
}

def test(enum: OtherEnum.Value) = {
    println(s"enum: $enum")
}

<console>:25: error: double definition:
def test(enum: Weekday.Value): Unit at line 21 and
def test(enum: OtherEnum.Value): Unit at line 25
have same type after erasure: (enum: Enumeration#Value)Unit
         def test(enum: OtherEnum.Value) = {
             ^
  • 在编译期间没有详尽的匹配检查(matching check)。下面的示例将在没有任何警告的情况下编译,但是在对周一和周日以外的工作日匹配时会抛出scala.MatchError异常:
代码语言:javascript复制
def nonExhaustive(weekday: Weekday.Value) {
  weekday match {
    case Monday => println("I hate Mondays")
    case Sunday => println("The weekend is already over? :( ")
  }
}

Scala中,我们严重依赖于编译器强大的类型系统,使用这种方法,编译器不能找到非穷尽模式匹配子句,也不能对不同的枚举使用重载方法。

为了避免这种问题,我们可以其他办法实现枚举:

  • 使用密封盒对象(sealed case objects)
  • 分项(itemized)
  • enumeratum

Sealed case objects

如果您决定使用sealed case objectsScala编译器可以解决Scala枚举中存在的两个问题。编译器既可以检测非穷尽模式匹配,也可以避免类型擦除问题。

代码语言:javascript复制
sealed trait Weekday

case object Monday extends Weekday
case object Tuesday extends Weekday
case object Wednesday extends Weekday
case object Thursday extends Weekday
case object Friday extends Weekday
case object Saturday extends Weekday
case object Sunday extends Weekday

def test(weekday: Weekday) = {
    weekday match {
      case Monday => println("I hate Mondays")
      case Sunday => println("The weekend is already over? :( ")
    }
}

<console>:15: warning: match may not be exhaustive.
It would fail on the following inputs: Friday, Saturday, Thursday, Tuesday, Wednesday
           weekday match {
           ^
test: (weekday: Weekday)Unit

另一个非常好的特性是,可以在枚举值中包含更多字段(Scala enumerations only provides an index and a name),仅仅使用sealed abstract class而不是sealed trait

代码语言:javascript复制
sealed abstract class Weekday( val name: String,
                               val abbreviation: String,
                               val isWorkDay: Boolean)

case object Monday extends Weekday("Monday", "Mo.", true)
case object Tuesday extends Weekday("Tuesday", "Tu.", true)
case object Wednesday extends Weekday("Wednesday", "We.", true)
case object Thursday extends Weekday("Thursday", "Th.", true)
case object Friday extends Weekday("Friday", "Fr.", true)
case object Saturday extends Weekday("Saturday", "Sa.", false)
case object Sunday extends Weekday("Sunday", "Su.", false)

sealed case objects的问题

但是这种方式也有它自己的问题:

  • 没有检索所有枚举值的简单方法
  • 没有默认的序列化/反序列化方法
  • 枚举值之间没有默认的排序——这可以通过包含一些关于值的信息来手动实现,示例如下:
代码语言:javascript复制
sealed abstract class Weekday( val name: String,
                               val abbreviation: String,
                               val isWeekDay: Boolean,
                               val order: Int) extends Ordered[Weekday] {

  def compare(that: Weekday) = this.order - that.order
}

case object Monday extends Weekday("Monday", "Mo.", true, 2)
case object Tuesday extends Weekday("Tuesday", "Tu.", true, 3)
case object Wednesday extends Weekday("Wednesday", "We.", true, 4)
case object Thursday extends Weekday("Thursday", "Th.", true, 5)
case object Friday extends Weekday("Friday", "Fr.", true, 6)
case object Saturday extends Weekday("Saturday", "Sa.", false, 7)
case object Sunday extends Weekday("Sunday", "Su.", false, 1)

scala> Monday < Tuesday
res0: Boolean = true

分项(itemized)

itemized是一个OSS lib,它是rbrick的一部分,rbricks是一种可组合的、占用空间小的Scala库的集合。

itemized为枚举提供了密封特质层次结构(sealed trait hierarchies)的宏和类型类,回到我们之前的例子:

代码语言:javascript复制
import io.rbricks.itemized.annotation.enum

@enum trait Weekday {
  object Monday
  object Tuesday
  object Wednesday
  object Thursday
  object Friday
  object Saturday
  object Sunday
}

除上面的以外,itemized还有其他的一些特性:

  • 列出所有枚举值
  • 默认的序列化/反序列化方法
代码语言:javascript复制
scala> import io.rbricks.itemized.ItemizedCodec

scala> ItemizedCodec[Weekday].fromRep("Monday")
res0: Option[Weekday] = Some(Monday)

scala> val weekday: Weekday = Planet.Monday

scala> import io.rbricks.itemized.ItemizedCodec.ops._

scala> weekday.toRep
res1: String = Earth

itemized的问题

尽管itemized可以让我们用注解方式创建类型安全的枚举,但是它也有一些不足:

  • 无法向枚举值添加更多字段(add more fields to enumeration values)。由于部分工作是由宏完成的,在这一点上,无法传递这些值
  • 尽管它提供了索引值,但枚举值仍然没有默认顺序(order on enumeration values)

enumeratum

Enumeratum是一个类型安全且功能强大的Scala枚举实现,它提供了详尽的模式匹配警告。

代码语言:javascript复制
import enumeratum._

sealed trait Weekday extends EnumEntry
object Weekday extends Enum[Weekday] {
  val values = findValues // mandatory due to Enum extension

  case object Monday extends Weekday
  case object Tuesday extends Weekday
  case object Wednesday extends Weekday
  case object Thursday extends Weekday
  case object Friday extends Weekday
  case object Saturday extends Weekday
  case object Sunday extends Weekday
}
代码语言:javascript复制
def test(weekday: Weekday) = {
    weekday match {
      case Weekday.Monday => println("I hate Mondays")
      case Weekday.Sunday => println("The weekend is already over? :( ")
    }
  }

<console>:18: warning: match may not be exhaustive.
It would fail on the following inputs: Friday, Saturday, Thursday, Tuesday, Wednesday
           weekday match {
           ^
test: (weekday: Weekday)Unit

除了非详尽的模式匹配警告,enumeratum还提供:

  • 列出可能的值(因为这些值需要在Enum继承上实现)
  • 默认的序列化/反序列化方法(有和没有异常抛出)
代码语言:javascript复制
scala> Weekday.withName("Monday")
res0: Weekday = Monday

scala> Weekday.withName("Momday")
java.util.NoSuchElementException: Momday is not a member of Enum (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
  at enumeratum.Enum$$anonfun$withName$1.apply(Enum.scala:82)
  at enumeratum.Enum$$anonfun$withName$1.apply(Enum.scala:82)
  at scala.Option.getOrElse(Option.scala:121)
  at enumeratum.Enum$class.withName(Enum.scala:81)
  at Weekday$.withName(<console>:13)
  ... 43 elided

scala> Weekday.withNameOption("Monday")
res2: Option[Weekday] = Some(Monday)

scala> Weekday.withNameOption("Momday")
res3: Option[Weekday] = None
  • 向枚举添加额外的值。它非常类似于我们给简单的密封盒对象添加额外的值
代码语言:javascript复制
sealed abstract class Weekday( val name: String,
                               val abbreviation: String,
                               val isWorkDay: Boolean) extends EnumEntry

case object Weekday extends Enum[Weekday] {
  val values = findValues
  case object Monday extends Weekday("Monday", "Mo.", true)
  case object Tuesday extends Weekday("Tuesday", "Tu.", true)
  case object Wednesday extends Weekday("Wednesday", "We.", true)
  case object Thursday extends Weekday("Thursday", "Th.", true)
  case object Friday extends Weekday("Friday", "Fr.", true)
  case object Saturday extends Weekday("Saturday", "Sa.", false)
  case object Sunday extends Weekday("Sunday", "Su.", false)
}
  • 排序可以通过与封闭层次(sealed hierarchies)结构相同的方式实现。只需与有序[]特质(trait)混合,并实现比较方法。
代码语言:javascript复制
sealed abstract class Weekday(val order: Int) extends EnumEntry with Ordered[Weekday] {
   def compare(that: Weekday) = this.order - that.order
 }

 object Weekday extends Enum[Weekday] {
   val values = findValues

   case object Monday extends Weekday(2)
   case object Tuesday extends Weekday(3)
   case object Wednesday extends Weekday(4)
   case object Thursday extends Weekday(5)
   case object Friday extends Weekday(6)
   case object Saturday extends Weekday(7)
   case object Sunday extends Weekday(1)
 }
  • 支持一些库和框架,这些库和框架在更大的应用程序上非常有用。项目文档链接:https://github.com/lloydmeta/enumeratum#table-of-contents

总结

如果您刚刚开始学习Scala,我建议使用scala.Enumeration的方式实现枚举。当您觉得使用更多Scala特性更舒服时,以及开始享受编译器安全性时,可以试试其他方式实现枚举。我的两个建议是:

  • 如果您不想依赖于外部库,就使用sealed hierarchies
  • 使用enumeratum,因为它提供了这里提到的所有特性

枚举特性总结

  • 详尽的模式匹配
  • 没有类型擦除
  • 安全的序列化/反序列化的默认方法
  • 列出所有可能的值
  • 在枚举值上添加额外的字段
  • 排序

编译自:Scala Enumerations

0 人点赞