Scala: Type Bounds

Reading Time: 3 minutes

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:
scala_upper_bound

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.

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:

scala_lower_bound

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.

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.

References:

 

 

Knoldus-blog-footer-image

 

Written by 

Ayush is a Software Consultant having more than 11 months of experience. He has knowledge of various programming languages like C, C++, Java, Scala, JavaScript and is currently working on Big Data Technologies like Spark, Kafka, ElasticSearch. He is always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies includes playing Cricket, Travelling, Watching Movies

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!