DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Compliance Automated Standard Solution (COMPASS), Part 10: How OSCAL Mapping Paves the Way for Continuous Compliance Scalability
  • Detecting Plan Regression in SQL Server Using Query Store
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  1. DZone
  2. Coding
  3. Languages
  4. Scala: Repeated Method Parameters

Scala: Repeated Method Parameters

Let's see how Scala supports variable arguments and repeated method parameters and the conditions to consider when using them.

By 
Gaurav Gaur user avatar
Gaurav Gaur
DZone Core CORE ·
Feb. 28, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
17.7K Views

Join the DZone community and get the full member experience.

Join For Free

Similar to Java, Scala also supports variable arguments or repeated method parameters. The concept is really useful in situations when you don't know how many parameters you need to pass to a method, or you have to pass an unlimited number of arguments to a method.

However, there are few conditions to using repeated method parameters in Scala

  • All the repeated parameters must be of the same type.

  • We can only have one argument as a repeated parameter in the method definition. We cannot declare 2 repeated parameters for a method.

  • Scala only allows the last parameter of the method call to be repeated.

To denote a repeated parameter, place an asterisk after the type of the parameter. For example, below is a sum method that would calculate the sum of all the numbers passed to the method.

def sum(args: Int*): Int = args.fold(0)(_+_)


You can call sum as sum() or sum(3,4) or sum(1,3,4,5,7,8,9). Scala treats incoming parameters as arrays. However, if you try to pass an array to sum(), Scala will throw a type mismatch error.

scala> sum(Array(1,2))
<console>:13: error: type mismatch;
 found   : Array[Int]
 required: Int
       sum(Array(1,2))


In order to pass an array, we need to append the argument with a colon and an _* symbol.

scala> sum(Array(1,2): _*)
res4: Int = 3


This notation will ask the compiler to pass each element of the array as a single argument. So array elements are passed one by one to sum(), rather than all of it as a single argument.

Please refer to this video to understand the concept in more detail and to check out a few more examples.


Scala (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook