Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, repeated parameters, also known as varargs (short for "variable number of arguments"), allow you to specify a method that can accept a variable number of arguments of the same type. They are analogous to Java's varargs.
When defining a method with repeated parameters, you use *
after the parameter's type.
def printAll(strings: String*): Unit = { strings.foreach(println) }
You can call this method with any number of String
arguments:
printAll() // No output printAll("One") // Outputs: One printAll("One", "Two") // Outputs: One followed by Two
As a Sequence: Inside the method, the varargs can be treated as a Seq
of the given type (in the example, Seq[String]
).
Passing a Sequence: If you already have a sequence and want to pass it as repeated parameters, you can use the :_*
type ascription.
val fruits = List("Apple", "Banana", "Cherry") printAll(fruits: _*)
Position: A repeated parameter must be the last parameter in the method.
// This is valid def example(strings: String*, number: Int): Unit = ??? // This is NOT valid def example(number: Int, strings: String*): Unit = ???
Only One Varargs: A method can have only one repeated parameter.
Repeated parameters are useful when you want to design functions or methods that can accept an arbitrary number of arguments, especially when the exact number of arguments is unknown or can change frequently.
For instance, the built-in println
function uses repeated parameters to allow for printing of multiple values of any type:
println("Hello", "World", 123, true)
Repeated parameters (varargs) provide a flexible way for method definitions that can handle an arbitrary number of arguments. By leveraging them judiciously, you can create more flexible and dynamic APIs in Scala.
How to Define and Use Repeated Parameters in Scala:
Repeated parameters allow a variable number of arguments of the same type.
def printNumbers(numbers: Int*): Unit = { numbers.foreach(println) } printNumbers(1, 2, 3, 4)
Passing Multiple Arguments to Repeated Parameters in Scala:
Multiple values can be passed to a repeated parameter.
def printStrings(strings: String*): Unit = { strings.foreach(println) } printStrings("Scala", "is", "awesome")
Mixing Repeated Parameters with Other Method Parameters in Scala:
Repeated parameters can be combined with other parameters.
def printInfo(prefix: String, numbers: Int*): Unit = { numbers.foreach(num => println(s"$prefix $num")) } printInfo("Number:", 1, 2, 3)
Handling Empty Repeated Parameters in Scala:
Repeated parameters can be empty, and the function still works.
def printWords(words: String*): Unit = { if (words.nonEmpty) words.foreach(println) else println("No words provided.") } printWords()
Named Arguments with Repeated Parameters in Scala:
Named arguments can be used with repeated parameters.
def printNames(names: String*): Unit = { names.foreach(println) } printNames(names = "Alice", "Bob", "Charlie")
Type Variance with Repeated Parameters in Scala:
Repeated parameters can be covariant.
def printAny[T](values: T*): Unit = { values.foreach(println) } printAny(1, "Scala", 3.14)
Scala Repeated Parameters vs. Arrays:
Repeated parameters provide a more convenient syntax compared to arrays.
def printArray(arr: Array[String]): Unit = { arr.foreach(println) } printArray(Array("one", "two", "three"))