Type Bound basically is a kind of restriction on the parameter or the variable. By the usage of these type bounds we can set up limits for the variables. These bounds help to put up our code into real world examples. We need to impose certain limitations and boundaries to every factor in real life, that is what the Type bounds do in Scala.
There are mainly 2 types of Type bounds:
We will be more concerned about Lower Bounds in this section. Lower type bounds are used to declare a supertype of another type. A lower bound is denoted by >: . Any lower bound statement can be written as ‘[T>:S]’. Here T is a type parameter and S is a type. The above lower bound statement means that the type parameter T must be either same as S of a supertype of S.
Lets understand about supertype, suppose we write S>:T then this lower bound statement always has T equal to S or less than S, meaning that T is bound in this case. So we can say T is a supertype of S.
[T >: Demo[T]]
In the above case we have defined a Lower Bound from type parameter T to type Demo. And T must be either a Demo or a supertype of Demo type.
Let’s take another Example
If I declare all the classes in the certain format as shown below:
trait Food class eatables extends Food class drinks extends Food class fruits extends eatables class vegetables extends eatables class cold_drinks extends drinks class hot_drinks extends drinks class position[A](val place:A)
Now all the classes as well as the sub-classes have been declared using the above statements. So if we consider the statement position[A], here position is generic and A is the abstract type. This can be further simplified, the position[A] indicates that the value required to pass to place must be of the type A. Which means that if I give a command position[fruits] then it will be created with an instance of Fruits. So to check this we will see one example:
new position[fruits](new fruits) new position[fruits](new drinks) new position[eatables](new vegetables)
In the above given example we will notice that the first statement will get compiled but the second statement will show errors because fruits and drinks are not of the same type. On the other hand the third statement will be compiled because vegetable is subtype of eatables. Now using some lower bound commands in the above given statements:
class position[A >: fruits](val place=A)
By this statement we can say that we can position any supertype of fruits which means eatables and Food. So, by this we can definitely see that the lower bounds of Scala can be very useful and adoptive if using some logic for any real world example. Thus we can add all the real life limitations and restrictions over our variables which we are using in the coding language.
GeeksforGeeks@506e1b77 Geeks@4fca772d Author@9807454
Here, Lower bound is defined in class ComputerSciencePortal, and GeeksforGeeks is the Super class of Author. it is accepted in lower bound.
Principal@506e1b77 Teacher@4fca772d Student@9807454
Here, we have defined Lower bound in Class School i.e, [T >: Teacher] which implies that display method accepts Principal class object or subclass of it(i.e. Teacher) or subclass of Teacher class (i.e. Student) which means that super class of Teacher will be accepted.
- Lower and Upper Bound Theory
- Scala | Upper bound
- Scala - View Bound
- Scala Tutorial – Learn Scala with Step By Step Guide
- Set in Scala | Set-2
- Scala Map
- Set in Scala | Set-1
- Scala | Either
- Recursion in Scala
- Scala Int !=(x: Int) method with example
- Scala | map() method
- while and do while Loop in Scala
- ListSet in Scala
- Scala Int >=(x: Int) method with example
- Scala Stream
- Scala | Final
- Scala Lists
- Scala | Multithreading
- BitSet in Scala
- Scala Int >>(x: Int) method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.