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
Newsletter
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

  • Why Spark Executors Leave Cores Idle on YARN: A Packing Model for Executor Sizing
  • AI Agents and Frontier Models: Insights From The Neuron
  • A Software Factory on Kubernetes: Gemini Builds, jev Decides (Genkit + senro in Go)
  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Feb. 28, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
17.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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