Scala Generics I:  Scala type bounds
Tags: agilewebandappde, abstracttypes, covariance, scala
Published: 2017-10-19 07:00:24

---
title: "Scala Generics I:  Scala type bounds"
published: true
tags: Agile web and app de,Abstract types,Covariance,Scala
canonical_url: https://apiumhub.com/tech-blog-barcelona/scala-type-bounds/
---

## Generic types in Scala

Generic types, abstract types, scala type bounds, all these concepts are unusual for software developers who are coming from languages in which the generics are not (or barely) used, so in this first article, we will discuss the basics and try to dig down only in type bounds.

LetÂ´s work with this little set of types and we will continuously modify the Parking type.

```
trait Thing 
trait Vehicle extends Thing 
class Car extends Vehicle 
class Jeep extends Car 
class Coupe extends Car 
class Motorcycle extends Vehicle 
class Vegetable 

class Parking[A](val place: A)
```

Nomenclature: If we think about Thing[A], Thing is a generic type, and (A) is the abstract type.

Before starting, we should understand how generic types work and the type inference in Scala.

Parking[A] indicates that the value we want to pass to “place must be of type A, so, if we create Parking[Motorcycle], we will&nbsp;create it with an instance of Motorcycle:

```
new Parking[Motorcycle](new Motorcycle)
```

If we try this:

```
new Parking[Motorcycle](new Car)
```

This won’t compile, obviously, since Car and Motorcycle are not the same type.  
 So, what happens when we try to park a Jeep in a “only car Parking lot?

```
new Parking[Car](new Jeep)
```

Jeep is subtype of Car,&nbsp; of course it works!

As I mentioned before, letÂ´s discuss the inference of types.   
 First, let’s change Parking a little bit:

```
class Parking[A](val place1: A, val place2: A)
```

If we follow the last example, Parking[Car] should receive two cars, two Jeeps, car and a Jeep, or something similar. But&nbsp;what&nbsp;happens if we donÂ´t specify the type?

```
new Parking(new Car, new Motorcycle)
```

It compiles and it is the compiler that assigns the type A. The type inference makes us the task easier but, what type is A now that we havenÂ´t indicated it?

The compiler will evaluate the type of both parameters, if they were the same type, it would be easy, however since they are not, it searches for the nearest common supertype.   
 An easy way to check this is to create this Parking on Scala REPL. The print follows clearly:

&nbsp;

```
val a = new Parking(new Car, new Motorcycle)
 *a: Parking[Vehicle] = Parking@278e1674*

Â 
```

It understood that “Parking must be a “Vehicle Parking”.

&nbsp;

## Scala type bounds

Once the introduction is done, letÂ´s bring on the table the interesting stuff.

In the previous example there weren’t restrictions, we could create Parking[Vegetables] and the world would keep spinning, however in real life we want to impose certain rules.   
 How can we create type parameter restrictions? To create them we&nbsp;make use of scala type bounds.

&nbsp;

### Upper type bounds

```
class Parking[A <: Vehicle]

Â 
```

The easier type bound to understand is upper type bound â€˜\<:’, this indicator would be the same as â€˜:’ when we create a value and we give it a specific type.   
 val a: Parking means that “a must be an instance of Parking or a subtype of Parking.&nbsp;  
 In the type scenario, Parking[A \<: Vehicle] means that the A type must be type or subtype of Vehicle.

Because of that, if we create a Vehicle, Car, Jeep or Motorcycle Parking, everything works.  
  
 The following lines could be added in a test and it the result would be SUCCESS:

```
new Parking[Vehicle] shouldBe a[Parking[_]]
 new Parking[Car] shouldBe a[Parking[_]]
 new Parking[Jeep] shouldBe a[Parking[_]]
 new Parking[Motorcycle] shouldBe a[Parking[_]]

Â 
```

But if we try to create a Vegetable or Thing Parking it won’t compile. Our compiler protects us:   
 If we add this lines in a test, it won’t even compile:

&nbsp;

```
new Parking[Vegetable] should be(a[Parking[_]])
new Parking[Thing] should be(a[Parking[_]])

* _ refers to existential types: https://typelevel.org/blog/2016/01/28/existential-inside.html
```

&nbsp;

### Lower type bounds

On the other hand we have the lower type bound â€˜\>:’, which indicates the opposite to â€˜\<:’.  
 [A \>: Vehicle] will restrict A to supertypes of Vehicle, Vehicle included.  
 Its uses are mainly related with co and contravariance even though this topic concerns to another article, we will break down the lower type bound concept:   
 LetÂ´s start understanding which type of relationship represents a lower type bound. A \>: B means that A must be B or higher from B, being B the frontier (bound).

```
class Parking[A >: Jeep](val place: A)
```

In this Parking we could park any supertype of Jeep, meaning, Car, Vehicle, Thing… AnyRef.. Well, it seems a little too generic, doesnÂ´t it?

```
class Parking[A >: Jeep <: Vehicle](val plaza: A)
```

Here is a way of lay it, if we imagine a type tree from that style:

```
trait Thing
class Vehicle extends Thing
class Car extends Vehicle
class Jeep extends Car
class Coupe extends Car
class Motorcycle extends Vehicle
class Bicycle extends Vehicle
class Tricycle extends Bicycle
```

Can we limit Parking to all the subtypes of Vehicles, above Tricycle?

```
class Parking[A >: Bicycle <: Vehicle](val plaza: A)
```

Looks like we do can, letÂ´s test it:

```
new Parking(new AnyRef)
```

&nbsp;

**\<console\>:12: error: inferred type arguments [Object] do not conform to class Parking’s type parameter bounds [A \>: Bicycle \<: Vehicle]**  
 **new Parking(new AnyRef)**  
 **^**  
 **\<console\>:12: error: type mismatch;**  
 **found : Object**  
 **required: A**  
 **new Parking(new AnyRef)**  
**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^**

AnyRef gives us a compilation error, perfect, we see that the upper type bound works.

&nbsp;

```
new Parking(new Bicycle)

res5: Parking[Bicycle] = Parking@1f3f425b
```

&nbsp;

```
new Parking(new Vehicle)
 res6: Parking[Vehicle] = Parking@61fb801b
```

&nbsp;

```
new Parking(new Coupe)
 res7: Parking[Vehicle] = Parking@e959286
```

&nbsp;

And we got to the Big Question, what will happen with Tricycle?

```
new Parking(new Tricycle)
res8: Parking[Bicycle] = Parking@53c20385
```

Probably some of you weren’t expecting that, but it works.   
 However, the compiler always has your back, and the type of Parking is now Parking[Bicycle].  
 Being Tricycle a subtype of Bicycle, the example has searched for a supertype which matches with the frontiers and it has found it. Because of Liskov there where you can&nbsp;use a Bicycle, you&nbsp;should be able to use a Tricycle as well.   
  
 However, this is not the most common use for lower type bounds,  
 They&nbsp;are used to put a covariant type in a contravariant position (in the next article, I will bring this concept up). Just so you have that clear, if you create a covariant Thing[+A], there are some rules that block you from using it in certain positions, such as:

```
class Parking[+A] {
 def parkIt(element: A): Parking[A] = new Parking(element)
}

Â 
```

Even though it looks correct, it is not: We are using A in a contravariant position even though it is a&nbsp;covariant type. Seriously, don’t worry if donÂ´t understand what it means, it will be explained in another article, just focus on the prohibition from the compiler.   
 So, how can we have a method which allows us to create Parking depending on the parameter type?   
 Lower type bounds:

&nbsp;

```
class Parking[+A] {
 def parkIt[B >: A](element: B): Parking[B] = new Parking(element)
 }

Â 
```

Because the lower type bound includes the frontier, adding a lower type bound we could use the covariant A type to generate the limit of the lower type bound from B and let B be the one who types everything. Tada!!!

&nbsp;

## Use-site variance

Another use for scala type bounds is Use-site variance. It is the form of variance that is used in Java.  
 Use-site variance consist in setting the&nbsp;bounds when&nbsp;declaring the type, if our Parking were invariant, it would be the unique way of doing any type of variance:

```
class Parking[A](val place: A){
 def dosomething(p1: Parking[_ <: Vehicle]) {}
}
```

Even though our Parking doesnÂ´t have Bounds nor covariance, it would accept any type being A, in our method dosomething we have created a limit, now we can do this:

```
dosomething(new Parking(new Car))
```

But we can NOT do this:

```
dosomething(new Parking(new AnyRef))
```

**\<console\>:12: error: type mismatch;**  
**found : Object**  
**required: Vehicle**  
**dosomething(new Parking(new AnyRef))**  
**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^**

&nbsp;

It would happen the same if we were using Thing, because AnyRef is a supertype&nbsp;of anytype that we can define.   
 If we wouldnÂ´t do use-site variance, this&nbsp;would happen:

```
def dosomething(p1: Parking[Vehicle]) {}
 var p1 = new Parking(new Car)
```

**dosomething(p1)**

**\<console\>:14: error: type mismatch;**  
**found : Parking[Car]**  
**required: Parking[Vehicle]**  
**Note: Car \<: Vehicle, but class Parking is invariant in type A.**  
**You may wish to define A as +A instead. (SLS 4.5)**  
**dosomething(p1)**  
**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^&nbsp;**

&nbsp;

It will only accept Parking[Vehicle]

The last thing i would like to mention is: be aware with type inference  
 If we don’t explicit&nbsp;the types, strange things can happen.&nbsp;We just witnessed how dosomething does not accept p1 because it wasn’t the Parking&nbsp;type it was expecting, but what will happen if we do this:

```
dosomething(new Parking(new Car))

Â 
```

The type inference will look for a matching type:&nbsp;it will rise the type of Car to Vehicle, creating a Parking[Vehicle]. Making everything work again.

With this tools you should we&nbsp;able to understand most of the codes where generics are used&nbsp;and even start writing them yourself.   
 In the following scala&nbsp;articles, I will keep discussing about generics, variance and type constraints. As well as the reasons of why using generics instead of polymorphism.&nbsp;

&nbsp;

If you are interested in Scala type bounds or in software development best practices, I recommend you to subscribe to [our monthly newsletter](http://eepurl.com/cC96MY) to receive latest tips.&nbsp;

&nbsp;

## If you found this article about Scala Type Bounds interesting, you might like…

&nbsp;

[BDD: user interface testing](https://apiumhub.com/tech-blog-barcelona/user-interface-testing/)

[F-bound over a generic type in Scala](https://apiumhub.com/tech-blog-barcelona/f-bound-scala-generics/)

[Microservices vs Monolithic architecture](https://apiumhub.com/tech-blog-barcelona/microservices-vs-monolithic-architecture/)

[“Almost-infinit scalability](https://apiumhub.com/tech-blog-barcelona/almost-infinite-scalability/)

The post [Scala Generics I: Scala type bounds](https://apiumhub.com/tech-blog-barcelona/scala-type-bounds/) appeared first on [Apiumhub](https://apiumhub.com).