Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Scala | Repeated Method Parameters

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.

Definition:

When defining a method with repeated parameters, you use * after the parameter's type.

def printAll(strings: String*): Unit = {
  strings.foreach(println)
}

Usage:

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

Using Repeated Parameters:

  1. As a Sequence: Inside the method, the varargs can be treated as a Seq of the given type (in the example, Seq[String]).

  2. 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: _*)
    

Limitations:

  1. 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 = ???
    
  2. Only One Varargs: A method can have only one repeated parameter.

When to Use:

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)

Conclusion:

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.

  1. 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)
    
  2. 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")
    
  3. 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)
    
  4. 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()
    
  5. 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")
    
  6. 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)
    
  7. 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"))