■ はじめに
Scala の パターンマッチング が強力って話が業務中にでてきたので 調べて、まとめる
目次
【0】パターンマッチング 【1】定数でのマッチング 【2】関数として定義 【3】データ型でのマッチング 【4】ケースクラスでのマッチング
【0】パターンマッチング
https://docs.scala-lang.org/ja/tour/pattern-matching.html
* パターンに照合するための仕組み * マッチに成功すれば、一つの値をその構成要素のパーツに分解することもできる * Javaのswitch文の強化バージョン
【1】定数でのマッチング
object DemoMain { def main(args: Array[String]): Unit = { val testValue = 0 # ★ここに注目★ val result = testValue match { case 0 => "NG" case 100 => "OK" case _ => "Not 0 or 100" } println(s"result: ${result}") } }
【2】関数として定義
object DemoMain { def matching(testValue: Int): String = testValue match { case 0 => "NG" case 100 => "OK" case _ => "Not 0 or 100" } def main(args: Array[String]): Unit = { val testValue = 100 val result = matching(testValue) println(s"result: ${result}") } }
【3】データ型でのマッチング
object DemoMain { def main(args: Array[String]): Unit = { val testValues = List(1, "Mike", true, 12.12) for (value <- testValues) { value match { case id: Integer => println(s"id=${id}") case name: String => println(s"name=${name}") case isMale: Boolean => println(s"isMale?=${isMale}") case _ => println(s"value=${value}") } } } }
【4】ケースクラスでのマッチング
abstract class Week case class Sunday() extends Week case class Monday() extends Week case class Tuesday() extends Week case class Wednesday() extends Week case class Thursday() extends Week case class Friday() extends Week case class Saturday() extends Week object DemoMain { def sayTodaysWeek(week: Week): String = { week match { case Saturday() => "Today is Saturday" case Sunday() => "Today is Sunday" case _ => "Today is boring day..." } } def main(args: Array[String]): Unit = { println(sayTodaysWeek(new Sunday)) } }
参考文献
https://qiita.com/f81@github/items/aa46c248a38a171ed955
関連記事
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/03/18/034704
Scala ~ 基本編 / Either型 ~
https://dk521123.hatenablog.com/entry/2023/08/02/132315
Scala ~ Enum ~
https://dk521123.hatenablog.com/entry/2023/01/05/000000