Scala Tutorial - Learn How To Create Typed Function

By Nadim Bahadoor | Last updated: July 25, 2017 at 13:46 pm

Overview

In this tutorial, we will learn how to create typed functions which will allow you to specify the types of the parameters when calling the function.

 

In addition, we will make use of what we've learned from the Pattern Matching Tutorial when creating our typed functions.

Steps

1. How to define a function which takes a String parameter

Let's start with a simple function to calculate the discount which our customers will receive when buying donuts from our store. The function will have a parameter of type String.

 

By now you should be familiar with how to define functions in Scala. If you do not know how to define functions in Scala, proceed to the tutorial on creating and using functions.


println("Step 1: How to define a function which takes a String parameter")
def applyDiscount(couponCode: String) {
  println(s"Lookup percentage discount in database for $couponCode")
}

 

2. How to define a function which takes a parameter of type Double

Similar to Step 1, let's define another function to calculate the discount for our customers. But this time our function will take a parameter of type Double.


println("\nStep 2: How to define a function which takes a parameter of type Double")
def applyDiscount(percentageDiscount: Double) {
  println(s"$percentageDiscount discount will be applied")
}

 

3. Calling applyDiscount function with String or Double parameter types

Calling the applyDiscount() function which takes a parameter of type String or Double should be familiar to you already if you've followed the tutorial on creating an using functions.


println("\nStep 3: Calling applyDiscount function with String or Double parameter types")
applyDiscount("COUPON_1234")
applyDiscount(10)

You should see the following output when you run your Scala application in IntelliJ:


Step 3: Calling applyDiscount function with String or Double parameter types
Lookup percentage discount in database for COUPON_1234
10.0 discount will be applied

 

4. How to define a generic typed function which will specify the type of its parameter

With the applyDiscount() function from Step 1 and Step 2 in mind, let us create another applyDiscount() function which will not specify the types of its parameters.

 

Instead, we will create a typed function which will specify a generic parameter of type T as follows:


println("\nStep 4: How to define a generic typed function which will specify the type of its parameter")
def applyDiscount[T](discount: T) {
  discount match {
    case d: String =>
      println(s"Lookup percentage discount in database for $d")

    case d: Double =>
      println(s"$d discount will be applied")

    case _ => 
      println("Unsupported discount type")
  }
}

NOTE:

  • We are making use of what we've learned from the Pattern Matching Tutorial to provide the corresponding code block if the parameter type is a String, Double or anything else.

5. How to call a function which has typed parameters

To call the typed function applyDiscount() from Step 4, you must provide the type of its parameters.


println("\nStep 5: How to call a function which has typed parameters")
applyDiscount[String]("COUPON_123")
applyDiscount[Double](10)

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to call a function which has typed parameters
Lookup percentage discount in database for COUPON_123
10.0 discount will be applied

This concludes our tutorial on Learn How To Create Typed Function and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to define a function which takes a String parameter
  • How to define a function which takes a parameter of type Double
  • Calling applyDiscount() function with String or Double parameter types
  • How to define a generic typed function which will specify the type of its parameter
  • How to call a function which has typed parameters

Tip

  • Scala's typed functions provide greater flexibility with the use of variances which we will see in upcoming tutorials.
  • However, if you would like to have a head start, feel free to visit the official documentation on variances.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to define polymorphic typed functions where the parameters as well as the return type is generic.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on