Scala Type Bounds: Upper Bounds, Lower Bounds and View Bounds With Examples [Latest]

Oct 24, 2022

In my previous post, I have discussed about Scala Variance in detail. In this post, we are going to discuss about “Scala Type Bounds”.

What is Type Bound in Scala?

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 following benefit:

• Type-Safe Application Development.

Scala Type Bounds

Scala supports the following Type Bounds for Type Variables:

• Scala Upper Bounds
• Scala Lower Bounds
• Scala View Bounds

We are going to discuss these concepts in detail with examples in next sections.

Scala Upper Bounds

In Scala, we can define Upper Bound on Type Parameter as shown below:

Description:-
Here T is a Type Parameter ans 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.

Example-1:-

Here We have defined Upper Bound from Type Parameter T to Type Ordered[T]. Then T much be either Ordered or subtype of Ordered type.

Example-2:-
Write a Scala program to demonstrate Scala Upper Bound.

This program works fine with commenting the following line.

If we uncomment this line and try to run it, we will get compilation error. Because we have defined Upper Bound as shown below:

Here we have defined “[T <: Dog]” that means “display” method accepts only either Dog class object or subclass type (i.e. Puppy) of Dog Class. That’s why if we pass Dog Super class, we will get “Type Mismatch” compilation error.

Scala Lower Bounds

In Scala, we can define Lower Bound on Type Parameter as shown below:

Description:-
Here T is a Type Parameter ans 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.

Example-1:-

Here We have defined Lower Bound from Type Parameter T to Type Ordered[T]. Then T much be either Ordered or supertype of Ordered type.

Example-2:-

Here Dog is not a subtype of Puppy, but still this program works fine because Dog is a subtype of Animal and we have defined “Lower Bound” on Type Parameter T as shown below:

If we remove Lower Bound definition in this class, then we will get some compilation errors.

Scala View Bounds

In Scala, View Bound is used when we want to use existing Implicit Conversions automatically. We can define View Bound on Type Parameter as shown below:

Description:-
In some scenarios, we need to use some Implicit conversions automatically to solve our problem statement. We can use Scala’s View Bounds to utilize these Implicits.

Example:-
Write a Scala program to compare Strings with Relational operators(like Int’s 10 > 12).

Output:-

If we don’t use Scala’s View Bound operator “<%”, then we will get the following error message.

That’s it all about Scala Upper Bounds, Lower Bounds and View Bounds. We will discuss some more Scala concepts in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions.