learning scala How To Create Variable Argument Function - varargs :_ *

Scala collection such as List or Sequence or even an Array to variable argument function using the syntax :_ *.

 

code : 

def printReport(names: String*) {
println(s"""Donut Report = ${names.mkString(" - ")}""")
}

println("\nStep 3: How to pass a List to a function with variable number of arguments")
val listDonuts: List[String] = List("Glazed Donut", "Strawberry Donut", "Vanilla Donut")
printReport(listDonuts: _*)

result:

Step 3: How to pass a List to a function with variable number of arguments
Donut Report = Glazed Donut - Strawberry Donut - Vanilla Donut

 

posted @ 2019-07-11 17:57  嵌入式实操  阅读(104)  评论(0编辑  收藏  举报