Scala Tutorial - Learn How To Create Polymorphic Function With Generic Return Type
Overview
In this tutorial, we will learn how to create polymorphic functions which will allow you to specify the types of the parameters as well as the return type of the function.
This tutorial is a continuation of the previous tutorial on Typed Functions where you learned how to create functions which have typed parameters.
Steps
1. Review how to define a generic typed function which will specify the type of its parameter
Let's first review what we've learned from the previous tutorial on Typed Functions. Typed functions allow you to define functions where you can specify the type of its parameters.
In the previous tutorial, we defined an applyDiscount() function which has a generic type parameter named discount.
println("\nStep 1: Review 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")
}
}
2. Review how to call a function which has typed parameters
To call the typed function applyDiscount() from Step 1, you have to specify the type of the discount parameter as follows:
println("\nStep 2: Review 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 2: Review how to call a function which has typed parameters
Lookup percentage discount in database for COUPON_123
10.0 discount will be applied
NOTE:
- Sure you can rely on Scala's type inference to infer the type of your function parameter.
- But in general it's a good practice to explicitly specify your parameter types.
3. How to define a polymorphic typed function which also has a generic return type
With the applyDiscount() function from Step 1 in mind, let us define another function which will be polymorphic in nature. We will name the function applyDiscountWithReturnType() and it will take a generic type T for its discount parameter. But it will also return a Sequence of the type T.
println("\nStep 3: How to define a generic typed function which also has a generic return type")
def applyDiscountWithReturnType[T](discount: T): Seq[T] = {
discount match {
case d: String =>
println(s"Lookup percentage discount in database for $d")
Seq[T](discount)
case d: Double =>
println(s"$d discount will be applied")
Seq[T](discount)
case d @ _ =>
println("Unsupported discount type")
Seq[T](discount)
}
}
NOTE:
- For simplicity, we are returning a new Sequence of type T for each of the cases within the pattern match.
4. How to call a generic polymorphic function which also has a generic return type
Let us call the polymorphic applyDiscountWithReturnType() function from Step 3 with different types namely String, Double and Char.
println("\nStep 4: How to call a generic typed function which also has a generic return type")
println(s"Result of applyDiscountWithReturnType with String parameter = ${applyDiscountWithReturnType[String]("COUPON_123")}")
println()
println(s"Result of applyDiscountWithReturnType with Double parameter = ${applyDiscountWithReturnType[Double](10.5)}")
println()
println(s"Result of applyDiscountWithReturnType with Char parameter = ${applyDiscountWithReturnType[Char]('U')}")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to call a generic typed function which also has a generic return type
Lookup percentage discount in database for COUPON_123
Result of applyDiscountWithReturnType with String parameter = List(COUPON_123)
10.5 discount will be applied
Result of applyDiscountWithReturnType with Double parameter = List(10.5)
Unsupported discount type
Result of applyDiscountWithReturnType with Char parameter = List(U)
NOTE:
- For each of the specified types used when calling the polymorphic function applyDiscountWithReturnType(), the function also returns the Sequence of the same type.
Summary
In this tutorial, we went over the following:
- Reviewed how to define a generic typed function which will specify the type of its parameter
- Reviewed how to call a function which has typed parameters
- How to define a polymorphic typed function which also has a generic return type
- How to call a generic polymorphic function which also has a generic return type
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.
- This tutorial was inspired by the official Scala documentation on Polymorphic Functions.
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 functions which take variable arguments.