Named Parameters and Default Parameter Values In Scala

Filed Under: Scala

In this post, we are going to discuss the following two important concepts in Scala about Function(Method) or Constructor Parameters

  1. Named Parameters
  2. Default Parameter Values

Introduction

Let’s assume that we are going to develop a HRMS (Human Resource Management System) module in Scala. It has the following Employee class.

Employee.scala


class Employee(val firstName: String, val lastName:String){    
     def fullName(delimiter:String):String = firstName + delimiter + lastName

     // Some useful code goes here
}

NOTE:- We will use this code snippet to explore this post objectives with simple examples.

In Scala, generally we make a Function (Method) call or Constructor call as shown below:

Constructor call In Scala


 scala> var emp1 = new Employee("Ram","Posa")
 emp1: Employee = Employee@563e4951

Method call In Scala


scala> emp1.fullName("-")
res1: String = Ram-Posa

If a Constructor or Function has too many Parameters of same type, then client may get confused about passing parameters to them. In our example, Employee class constructor has two parameters of same Type : String. In this case, Client may get confused about FirstName and LastName order.

If client pass parameters in different order as shown below, we will get some unexpected results. It does not give any compilation or runtime errors but produce some unexpected results


 scala> var emp1 = new Employee("Posa","Ram")
 emp1: Employee = Employee@563e4951

 scala> emp1.fullName("-")
 res1: String = Posa-Ram

To solve This problem, Scala have some features like Named Parameters and Default Parameter values. We will discuss these two concepts in next sections.

What is Named Parameter

Named Parameter means when making a Function (Method) call or Constructor call, we use name of the Parameter (Argument or Variable) explicitly.

Constructor Call with Named Parameters Syntax:-


  Constructor-Name(Parameter1-Name = Value1,  Parameter2-Name = Value2)

This syntax is very simple. Each Parameter value is preceded by a Parameter Name. Both Parameter Name and Parameter Value are separated by an equal (“=”) sign.

We use same syntax even for making a Function (Method) call with Named Parameters.

Named Parameters In Scala

Before Scala 2.8, we have this problem. To solve these problems, Scala 2.8 has introduced a new Feature called “Named Parameters” to make a Function call or Constructor call easily with too many Parameters (Arguments) of the same type.

Example:-
Take our previous HRMS Employee component to explore this syntax.


  scala> var emp2 = new Employee(firstName="Ram", lastName="Posa")
  emp2: Employee = Employee@569e4901

  scala> emp2.fullName("-")
  res2: String = Ram-Posa

If we use Name Parameters, then we don’t need to worry about Parameters order. Even though, We make a call with different order, still we will get same results as shown below.


  scala> var emp3 = new Employee(lastName="Posa",f irstName="Ram")
  emp3: Employee = Employee@511e4001

  scala> emp3.fullName("-")
  res2: String = Ram-Posa

NOTE:- If we use all Named Parameters to make a Constructor or Function call, then there is no issue about the order of the parameters. We can pass them in any order as per our convenience. But if we have any Unnamed Parameters, then we should declare Unnamed Parameters first, then Named Parameters.

Example:-
Suppose, our HRMS Employee component have few more Unnamed constructor parameter like empid as shown below:


class Employee( val empid:Int, val firstName: String, val lastName:String){    
     def fullName(delimiter:String):String = firstName + delimiter + lastName

     // Some useful code goes here
}

In this case, we have few options to make Employee Constructor call.

  • Use Named Parameters for all Arguments of Employee Constructor and use them in any order.
  • Example:-

    
     scala> var emp4 = new Employee(lastName="Posa", firstName="Ram", empid=1001)
     emp4: Employee = Employee@50378a4
    
  • If we mix Named and Unnamed Parameters, then that unnamed parameter(s) should come first.
  • 
     scala> var emp5 = new Employee(1001, firstName="Ram", lastName="Posa")
     emp5: Employee = Employee@6d5620ce
    

NOTE:- Unnamed Parameters are also known as “Positional Parameters” as they use positions to make Function or Constructor calls.

Advantages of Named Parameters In Scala

In Scala, Named Parameters have the following Advantages:

  • It avoids Client confusion in passing Parameters to Method or Constructor calls.
  • It improve the readability of the code.

NOTE:- Java doesn’t support Named Parameters. However, we can implement this approach by using Builder Design Pattern.

Difference between Named and Unnamed Parameters

  • Named Parameters uses Parameter Names to make Function or Constructor calls. Unnamed Parameters uses Parameter Positions to make Function or Constructor calls.
  • We can pass Named Parameters in any order. We should follow same order for Unnamed Parameters.

NOTE:- We can mix Named and Positional (Unnamed) Parameters to make Function or Constructor calls.

In Real-time Scala Based applications, we use Named Parameters with Default Parameters. We will discuss it in next section.

What is Default Parameter

Default Parameter means assigning some default value at Parameter definition in a Function or Constructor.

Constructor with Default Parameters Syntax:-


  Class-Name(Parameter1-Name : Type = Default-Value1, 
            Parameter2-Name : Type = Default-Value2)

This syntax is very simple. Each Parameter Default value is preceded by a Parameter Name and Type. Both Parameter Name and Parameter Value are separated by an equal (“=”) sign.

We use same syntax even for defining a Function(Method) with Default Parameters.

Default Parameter Values In Scala

Scala 2.8 has introduced one more new Feature called “Default Parameters” to make a Function call or Constructor call easier.

Example:-

Employee.scala


class Employee(val empid:Int, val firstName: String, val lastName:String){    
     def fullName(delimiter:String = " "):String = firstName + delimiter + lastName

     // Some useful code goes here
}

Here fullName method has a Default Parameter “delimiter” with some default value.


scala> var e1 = new Employee(1002, lastName = "Posa", firstName="Ram")
e1: Employee = Employee@93081b6

scala> e1.fullName("-")
res1: String = Ram-Posa

scala> e1.fullName()
res3: String = Ram Posa

scala>

If we required, we can make a Function call with Parameters. Otherwise, we use default parameters.

Here we are using both Named Parameters to make a Constructor call and Default Parameters to make a Function call.

Simple Syntax:- Parameter-Name: Type = Expression-Or-Value

We can also use an expression in defining Default Parameters. That expression is evaluated every time we make a call to that Function or Constructor.

NOTE:- We can mix both Named & Unnamed Parameters as well as Default & Non-Default Parameters.

Advantages of Default Parameters In Scala

In Scala, Default Parameters have the following Advantages:

  • It is possible to implement Optional Parameters.
  • It avoids code duplication.

NOTE:- Java doesn’t support Default Parameters concept. To support this, we need to use Overloading Concept to provide different Constructors or Methods.

That’s it all about Named Parameters and Default Parameter values in Scala. We will discuss some more Scala concepts in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions.

Comments

  1. puneet says:

    quite crisp and useful information about arguments

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages