Scala Tutorial - Learn How To Create Covariance Type Class

By Nadim Bahadoor | Last updated: March 4, 2019 at 18:11 pm

Overview

In this tutorial, we will continue from what we've learned from the previous tutorials on Class InheritanceCase Class Inheritance and Type Class in Scala.

 

The examples in this tutorial will be a review of inheritance in Scala with classes and case classes which extend an abstract class. In addition, we will also define a class which expects a given type - type class.

 

To follow on from the previous tutorial on Type Class, we will show example of enforcing type hierarchies through the use of variance - in particular covariance.

 

If you recall from the tutorial on What is Scala programming language, Scala is both an Object Oriented and Functional programming language. As a result of its Object Oriented nature, it has full support of object hierarchies through the use of class inheritance.

 

Steps

1. How to define an abstract class called Donut 

When dealing with class and object hierarchies, you typically have a base class which encapsulates common behaviour. As an example, let's create an abstract class name Donut which defines a method signature for printName.


println("Step 1: How to define an abstract class called Donut")
 abstract class Donut(name: String) {

   def printName: Unit

}

NOTE:

  • Any class which extends the abstract class Donut will have to provide an implementation for the printName method.

2. How to extend abstract class Donut and define a case class called VanillaDonut

To create a sub-class of the abstract Donut class from Step 1, you have to use the extends keyword, pass-through the name constructor parameter and also provide an implementation for the printName method.

 

As shown in the example below, the case class VanillaDonut extends the abstract class Donut and also provides an implementation for the printName method.


println("\nStep 2: How to extend abstract class Donut and define a case class called VanillaDonut")
 case class VanillaDonut(name: String) extends Donut(name) {

   override def printName: Unit = println(name)

 }

NOTE:

3. How to extend abstract class Donut and define another case class of Donut called GlazedDonut

Similar to Step 2, let's create another case class named GlazedDonut which extends the abstract Donut class.



println("\nStep 3: How to extend abstract class Donut and define another case class called GlazedDonut")
 case class GlazedDonut(name: String) extends Donut(name) {

   override def printName: Unit = println(name)

 }

NOTE:

  • Similar to Step 2, we did not have to create a Companion Object for the GlazedDonut class.

4. How to instantiate Donut objects

Let's create two Donut objects, one using the VanillaDonut class and the other one using the GlazedDonut class. Note that we are specifying the type for both vanillaDonut and glazedDonut to be of base type Donut.


println("\nStep 4: How to instantiate Donut objects")
val vanillaDonut: VanillaDonut = VanillaDonut("Vanilla Donut")
vanillaDonut.printName

val glazedDonut: Donut = GlazedDonut("Glazed Donut")
glazedDonut.printName

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


Step 4: How to instantiate Donut objects
Vanilla Donut
Glazed Donut

NOTE:

  • Since VanillaDonut and GlazedDonut are sub-classes of the base class Donut, they both have access to the printName method.

5. How to define a ShoppingCart type class which expects Donut types

The above steps should be familiar already if you have followed the previous tutorials on Class Inheritance and Case Class Inheritance.

 

What if you had to define a class which expects a particular type parameter? As an example, let us define a ShoppingCart class which expects a sequence of Donut types.


println("\nStep 5: How to define a ShoppingCart type class which expects Donut types")
class ShoppingCart[D <: Donut](donuts: Seq[D]) {

  def printCartItems: Unit = donuts.foreach(_.printName)

}

NOTE:

  • With the notation [D <: Donut], we are restricting only Donut types to be passed-through to the ShoppingCart class.

6. How to create instances or objects of ShoppingCart class

The example below shows how to instantiate a ShoppingCart class of type Donut.


println("\nStep 6: How to create instances or objects of ShoppingCart class")
val shoppingCart: ShoppingCart[Donut] = new ShoppingCart(Seq[Donut](vanillaDonut, glazedDonut))
shoppingCart.printCartItems

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


Step 6: How to create instances or objects of ShoppingCart class
Vanilla Donut
Glazed Donut

NOTE:

  • You will get compiler error if you try to instantiate a ShoppingCart class with a different type other than the Donut type.
  • As an example, you will get a compiler error if you tried to create ShoppingCart of type String

val shoppingCart2: ShoppingCart[Donut] = new ShoppingCart[String](Seq("Vanilla Donut"))
  • But what if you need to create a ShoppingCart of type VanillaDonut? You will get a compiler error for a ShoppingCart of type VanillaDonut:

val shoppingCart: ShoppingCart[Donut] = new ShoppingCart[VanillaDonut](Seq(vanillaDonut))

 

7. How to enable covariance on ShoppingCart

The example below shows how to instantiate a ShoppingCart2 class of type VanillaDonut by first enabling covariance for Donut types on ShoppingCart2 class.


println(s"\nStep 7: How to enable covariance on ShoppingCart")
 class ShoppingCart2[+D <: Donut](donuts: Seq[D]) {

 def printCartItems: Unit = donuts.foreach(_.printName)

 }

 val shoppingCart2: ShoppingCart2[Donut] = new ShoppingCart2[VanillaDonut](Seq(vanillaDonut))
 shoppingCart2.printCartItems

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


Step 7: How to enable covariance on ShoppingCart
Vanilla Donut

NOTE:

  • We've enabled covariance of type Donuts using the notation [+D <: Donut]
  • In other words, you can now create instances of ShoppingCart of type Donut or sub-types of Donuts such as VanillaDonut.

This concludes our tutorial on Learn How To Create Covariance Type Class 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 an abstract class called Donut
  • How to extend abstract class Donut and define a case class called VanillaDonut
  • How to extend abstract class Donut and define another case class of Donut called GlazedDonut
  • How to instantiate Donut objects
  • How to define a ShoppingCart type class which expects Donut types
  • How to create instances or objects of ShoppingCart class
  • How to enable covariance on ShoppingCart

Tip

  • As we've seen in this tutorial, Scala provides support for the traditional Object Oriented approach regarding class inheritance by extending classes.
  • Avoid having a case class extend other case classes. Instead, encapsulate common behaviour in an abstract class - see Scala Documentation.
  • However, in Chapter 5, we will show how Scala provides greater flexibility to class inheritance by making use of mix-in with traits.
  • Scala Documentation on variance.

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 specify contravariance to type classes in Scala.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: