3

I'm learning Scala and I just faced variadic functions. It's almost all ok on the example below I wrote:

object Sscce {

  def main(args: Array[String]) {
    printStrings("Hello", "Scala", "here", "I", "am");
  }

  def printStrings(ss: String*): Unit = {
    if (!ss.isEmpty) {
      println(ss.head)
      printStrings(ss.tail: _*)
    }
  }

}

I understand that String* means a variable list of strings and that ss is mapped to a Seq type. I also assume that a Seq cannot passed to a variadic function so in the recursive call of printStrings something has to be done with ss.

Question is: what is the exact meaning of : _*? It seems something like a cast to me (since there's the : symbol)

giampaolo
  • 6,906
  • 5
  • 45
  • 73

1 Answers1

1

It's called a sequence argument (see the second-to-last paragraph in section 6.6 Function Applications of the Scala Language Specification).

It deliberately resembles Type Ascription syntax. In an argument list, it basically means the exact opposite of what Repeated Parameters mean in a parameter list. Repeated parameters mean "take all the leftover arguments and collect them into a single Seq", whereas sequence arguments mean "take this single Seq and pass its elements as individual arguments".

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653