【Scala】Scala ~ 可変長引数のメソッドでネストした際の注意点 ~

◾️はじめに

久しぶりにコードを書く仕事にありつけた、、、

https://dk521123.hatenablog.com/entry/2024/07/30/155036

で、Scalaの可変長引数をやったが、
Javaのように使ったら単体試験でエラーになって大分ハマったので
メモしておく

目次

【1】Scalaで可変長引数のメソッドでネストした場合
【2】対応案
 1)修正例
【3】サンプル

【1】Scalaで可変長引数のメソッドでネストした場合

http://naoki-k.blogspot.com/2011/10/blog-post.html

に記載されている通り、
WrappedArray という型(ScalaのバージョンによってはArraySeq??)で
渡っているため意図した結果にならない

1)現象発生例

val result = getMessage("Hello")
println(result)

def getMessage(values: String*): String = {
  "%s".format(values)
}

https://scastie.scala-lang.org

で実行したら「ArraySeq(Hello)」

【2】対応案

* 可変長引数のメソッドでネストする場合、
 「_*」シンボルを指定する必要がある

1)修正例

val result = getMessage("Hello")
println(result)

def getMessage(values: String*): String = {
  // ★ここに注目(「values: _*」)
  "%s".format(values: _*)
}

https://scastie.scala-lang.org

で実行したら「Hello」(OK動作!)

【3】サンプル

object Hello {
  def main(args: Array[String]): Unit = {
    // toList
    val result1s = toList("Hello", "world", "")
    println(result1s.get.mkString(","))

    println("Done")
  }

  def toList(values: String*): Option[List[String]] = {
    // ★(values: _*)部分★
    if (values.isEmpty) None else toListWithFilter(values: _*)
  }

  def toListWithFilter(values: String*): Option[List[String]] = {
    if (values.isEmpty)
      None
    else {
      val list = values.filter(x => x.nonEmpty).map(x => x).toList
      if (list.isEmpty) None else Some(list)
    }
  }
}

参考文献

https://jumble-note.blogspot.com/2013/06/scala.html
http://naoki-k.blogspot.com/2011/10/blog-post.html

関連記事

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/2024/07/30/155036
Scala ~ 基本編 / Option型 ~
https://dk521123.hatenablog.com/entry/2023/03/09/000000
Scala ~ 基本編 / ジェネリック
https://dk521123.hatenablog.com/entry/2023/03/21/003817
Scala ~ 基本編 / コレクション ~
https://dk521123.hatenablog.com/entry/2023/03/13/000345
Scala ~ コレクションで使えるメソッド ~
https://dk521123.hatenablog.com/entry/2023/09/07/223422
Scala ~ mutable collection ~
https://dk521123.hatenablog.com/entry/2024/07/09/223730
Scala ~ 基本編 / メソッド ~
https://dk521123.hatenablog.com/entry/2023/03/03/000000
Scala ~ 基本編 / 繰り返し ~
https://dk521123.hatenablog.com/entry/2023/01/24/000000
Scala ~ 基本編 / トレイト ~
https://dk521123.hatenablog.com/entry/2023/09/10/204016
Scala ~ 基本編 / パターンマッチング ~
https://dk521123.hatenablog.com/entry/2023/06/06/233614
Scala ~ 基本編 / 例外処理 ~
https://dk521123.hatenablog.com/entry/2023/10/05/000135
ScalaEnum
https://dk521123.hatenablog.com/entry/2023/01/05/000000
Scala ~ asInstanceOf / isInstanceOf ~
https://dk521123.hatenablog.com/entry/2024/07/26/225713
Scala ~ implicit ~
https://dk521123.hatenablog.com/entry/2024/07/24/120726
Scala ~ 代数的データ型 / ADT ~
https://dk521123.hatenablog.com/entry/2024/07/23/193246
Scala ~ Joda-Time ~
https://dk521123.hatenablog.com/entry/2024/07/10/001348
ScalaTest ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/27/001306
ScalaTest ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/03/28/003906
ScalaTest ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2024/06/11/232020
SBTでのエラー/警告対応
https://dk521123.hatenablog.com/entry/2023/04/06/093458