【Scala】Scala ~ Enum ~

■ はじめに

昔っから、プログラムを書くときに
Enumを相当活用するのだが、
昨今のプログラム言語では、Enumを軽視してて
少し悲しい。。。
圧倒的に可読性や利便性があがると思うのだが、、、

今回は、Scala の Enum (列挙型) を扱う

目次

【0】ScalaでのEnum
【1】標準Enumerationを使用する場合
 1)使用上の注意
【2】sealed trait/case objectの組み合わせて定義する場合
【3】enumeratumを使用する場合
 1)インストール
 2)サンプル

【0】ScalaでのEnum

* 書き方は色々ある
 => 一応、標準でも搭載されている
 => 全部やってみたが、依存ライブラリが増えるのが嫌でなければ
 「【3】enumeratumを使用する場合」がいいと思う

【1】標準Enumerationを使用する場合

object DbDataType extends Enumeration {
  val IntType, LongType, StringType, DateType, TimestampType = Value
}

使い方

val dataType = DbDataType.IntType

サンプル

object Currency extends Enumeration {
  val JPY = Value("Japan")
  val USD = Value("USA")
  val EUR = Value("Euro")
  val AUD = Value("Australia")
}

object  Hello {
  def main(args: Array[String]): Unit = {
    println(Currency.JPY) // Japan
  }
}

1)使用上の注意

* Javaとは違い、メソッドが定義できない

【2】sealed trait/case objectの組み合わせて定義する場合

// sealed trait(またはabstract class)とcase objectの組み合わせで定義する
sealed trait DbDataType

object DbDataType {
  case object IntType extends DbDataType
  case object LongType extends DbDataType
  case object StringType extends DbDataType
  case object DateType extends DbDataType
  case object TimestampType extends DbDataType

  lazy val values: IndexedSeq[DbDataType] = 
    IndexedSeq(
      DbDataType.IntType,
      DbDataType.LongType,
      DbDataType.StringType,
      DbDataType.DateType,
      DbDataType.TimestampType
    )
}

使い方

val dataType:DbDataType = DbDataType.IntType

【3】enumeratumを使用する場合

1)インストール

libraryDependencies ++= Seq(
  "com.beachape" %% "enumeratum" % "1.7.2",

2)サンプル

import enumeratum.{Enum, EnumEntry}
import scala.collection.immutable

object  Hello {
  def main(args: Array[String]) {
    val stringValue = DbDataType.LongType.entryName
    println(s"result = ${stringValue}")

    val value = DbDataType.withName("IntType")
    println(s"result = ${value}")

    val id = DbDataType.TimestampType.id
    println(s"result = ${id}")

    for (enumValue <- DbDataType.values) {
      println(enumValue)
    }
    println("Done...")
  }
}

sealed abstract class DbDataType(val id: Int) extends EnumEntry

object DbDataType extends Enum[DbDataType] {
  case object IntType extends DbDataType(1)
  case object LongType extends DbDataType(2)
  case object StringType extends DbDataType(3)
  case object DateType extends DbDataType(4)
  case object TimestampType extends DbDataType(5)

  override def values: immutable.IndexedSeq[DbDataType] = findValues
}

出力結果

result = LongType
result = IntType
result = 5
IntType
LongType
StringType
DateType
TimestampType
Done...

参考文献

http://xerial.org/scala-cookbook/recipes/2012/06/29/enumeration
https://qiita.com/ka2kama/items/7b32667d6ad0995655ca

関連記事

Scala ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/03/10/193805
Scala ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/12/184331
Scala ~ 基本編 / 繰り返し ~
https://dk521123.hatenablog.com/entry/2023/01/24/000000
Scala ~ 基本編 / Option型 ~
https://dk521123.hatenablog.com/entry/2023/03/09/000000
Scala ~ 基本編 / メソッド ~
https://dk521123.hatenablog.com/entry/2023/03/03/000000
Scala ~ 基本編 / クラス ~
https://dk521123.hatenablog.com/entry/2023/03/14/000857
Scala ~ 基本編 / コレクション ~
https://dk521123.hatenablog.com/entry/2023/03/13/000345
Scala ~ 基本編 / 日付・日時 ~
https://dk521123.hatenablog.com/entry/2023/03/08/000000
Scala ~ 基本編 / パターンマッチング ~
https://dk521123.hatenablog.com/entry/2023/06/06/233614
Scala ~ 基本編 / 正規表現
https://dk521123.hatenablog.com/entry/2023/03/18/034704
Scala ~ 基本編 / ジェネリック
https://dk521123.hatenablog.com/entry/2023/03/21/003817
Scala ~ ファイル名・パスの扱い ~
https://dk521123.hatenablog.com/entry/2023/03/11/000000
Scala ~ ファイルハンドリング ~
https://dk521123.hatenablog.com/entry/2023/01/03/000000
ScalaYAML
https://dk521123.hatenablog.com/entry/2023/03/16/012034
ScalaAWS SDK
https://dk521123.hatenablog.com/entry/2023/03/24/211033
SBT ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/22/000000
SBT ~ 基本編 / build.sbt ~
https://dk521123.hatenablog.com/entry/2023/01/27/000000
SBT ~ 基本編 / sbtコマンド ~
https://dk521123.hatenablog.com/entry/2023/01/26/000000
SBT ~ sbtプラグイン
https://dk521123.hatenablog.com/entry/2023/01/25/000000
JavaでEmail ~ JavaMail / Text ~
https://dk521123.hatenablog.com/entry/2016/07/16/222422
JavaでEmail ~ JavaMail / 添付ファイル ~
https://dk521123.hatenablog.com/entry/2016/07/17/023459
JavaでEmail ~ SMTP認証 ~
https://dk521123.hatenablog.com/entry/2016/11/07/215251
JavaでEmail ~ SMTP認証 / DIGEST-MD5
https://dk521123.hatenablog.com/entry/2016/12/07/222229
JavaでEmail ~ JavaMail / TLS
https://dk521123.hatenablog.com/entry/2017/05/03/163219
JavaでEmail ~ JavaMail / Return-Path・Errors-To ~
https://dk521123.hatenablog.com/entry/2017/05/07/000344
Amazon SES ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2017/04/28/234103
Amazon S3 ~ Boto3編 ~
https://dk521123.hatenablog.com/entry/2019/10/21/230004
Amazon S3 ~ Boto3でファイル存在チェック ~
https://dk521123.hatenablog.com/entry/2022/02/26/182526
AWS Glue ~ Scalaでの実装 ~
https://dk521123.hatenablog.com/entry/2023/03/17/000000
AWS Glue ~ ローカル環境を作成する / Glue v3.0版 ~
https://dk521123.hatenablog.com/entry/2022/01/31/165650
LocalStack ~ ローカルで疑似AWSを作成する ~
https://dk521123.hatenablog.com/entry/2019/12/14/010524
LocalStack ~ ローカルで疑似Lambda/S3/DynamoDBを作成する ~
https://dk521123.hatenablog.com/entry/2019/12/16/231149
SparkからSnowflakeへの接続について考える
https://dk521123.hatenablog.com/entry/2023/03/19/013833