Connecting...

Pexels Photo 278888

Scala: Type Bounds by Ayush Hooda

Pexels Photo 278888

What are type bounds used in Scala for? 

They give us the benefit of type-safe application development and help us define the limits of a Type Variable! Want to know more? Check out this article by Software Consultant, Ayush Hooda and get in the know about type bounds!

 

'As we have already discussed in my last blog about the Type Parameterization concepts like Generics, Generic classes, and Variance. So to follow up, in this blog we’ll be discussing type bounds.

 

Type Bounds:

How can we create type parameter restrictions? To create them we make use of Scala type bounds. In Scala, Type Bounds are restrictions on Type Parameters or Type Variable. By using Type Bounds, we can define the limits of a Type Variable. Scala Type Bounds give us the benefit of Type-Safe Application Development.
Scala supports the following Type Bounds for Type Variables:
  • Scala Upper Bounds
  • Scala Lower Bounds

We’ll be discussing these types in detail with examples one by one.

 

Upper Type Bounds:

In Scala, we define upper bound on the Type parameter as shown below:

Here T is a Type Parameter and S is a type. By declaring Upper Bound like [T <: S] means this Type Parameter T must be either same as S or Sub-Type of S. Let us understand this with the help of an example.
1  abstract class Animal {
2  def name: String
3  }
4
5  abstract class Pet extends Animal
6
7  class Cat extends Pet {
8  override def name: String = "Cat"
9  }
10
11 class Dog extends Pet {
12 override def name: String = "Dog"
13 }
14
15 class Lion extends Animal {
16 override def name: String = "Lion"
17 }
18
19 class PetContainer[T <: Pet](t: T) {
20 def pet: T = t
21 }
22
23 object UpperTypeBounds extends App {
24 val dogContainer = new PetContainer[Dog](new Dog)
25 val catContainer = new PetContainer[Cat](new Cat)
26
27 // this would not compile
28 val lionContainer = new PetContainer[Lion](new Lion)
29
30 }

Here, PetContainer class takes a type parameter T which has an upper bound Pet i.e., T can be either Pet or a subtype of Pet. Therefore, dogContainer and catContainer are valid values whereas lionContainer is not as Lion is not a subtype of Pet.

 

Lower Type Bounds:

In Scala, we define upper bound on the Type parameter as shown below:

Here T is a Type Parameter and S is a type. By declaring Lower Bound like [T >: S] means this Type Parameter T must be either same as S or Super-Type of S. Let us understand this with the help of an example.

1  trait Thing
2
3  class Vehicle extends Thing
4
5  class Car extends Vehicle
6
7  class Jeep extends Car
8
9  class Coupe extends Car
10
11 class Motorcycle extends Vehicle
12
13 class Bicycle extends Vehicle
14 
15 class Tricycle extends Bicycle
16
17 // We need to restrict parking to all subtypes of vehicle, above Tricycle
18 
19 class Parking[T >: Bicycle <: Vehicle](val plaza: T)
20
21 object LowerTypeBounds extends App {
22
23   // this won't compile
24   val parking1 = new Parking[AnyRef](new AnyRef)
25
26   // this will compile
27   val parking2 = new Parking[Bicycle](new Bicycle)
28 
29   // this won't compile
30   val parking3 = new Parking[Thing](new Thing{})
31
32 }

Here, we try to restrict the parking for subtypes of Vehicle above Tricycle. So we have declared an upper bound Vehicle and a lower bound Bicycle. Therefore, parking1 and parking3 values simply won’t compile as AnyRef is a supertype of reference types and we have restricted the upper bound to Vehicle and Thing is also a supertype of Vehicle. parking2 works absolutely fine as it lies in the range of Bicycle and Vehicle.

 

Conclusion:

So to summarize, Type Bounds are nothing but used to enforce the restrictions on the Type parametrizations. Scala provides two types of Type Bounds namely Lower Type Bounds which are basically used to restrict the subtypes and Upper Type Bounds which are used to restrict the supertypes.

For Full Scala tutorial refer to my Github repository Scala Tutorial.'

This article was written by Ayush Hooda and posted originally on Knoldus Blog.