Previous

Next

Tour of Scala

Upper Type Bounds

In Scala, type parameters and abstract types may be constrained by a type bound. Such type bounds limit the concrete values of the type variables and possibly reveal more information about the members of such types. An upper type bound T <: A declares that type variable T refers to a subtype of type A. Here is an example that demonstrates upper type bound for a type parameter of class PetContainer:

In [1]:
abstract class Animal {
 def name: String
}

abstract class Pet extends Animal {}

class Cat extends Pet {
  override def name: String = "Cat"
}

class Dog extends Pet {
  override def name: String = "Dog"
}

class Lion extends Animal {
  override def name: String = "Lion"
}

class PetContainer[P <: Pet](p: P) {
  def pet: P = p
}

val dogContainer = new PetContainer[Dog](new Dog)
val catContainer = new PetContainer[Cat](new Cat)
Out[1]:
defined class Animal
defined class Pet
defined class Cat
defined class Dog
defined class Lion
defined class PetContainer
dogContainer: PetContainer[Dog] = [email protected]
catContainer: PetContainer[Cat] = [email protected]
In [2]:
// this would not compile
val lionContainer = new PetContainer[Lion](new Lion)
cmd1.sc:104: type arguments [cmd1.this.cmd0.Lion] do not conform to class PetContainer's type parameter bounds [P <: Helper.this.Pet]
val lionContainer = new PetContainer[Lion](new Lion)
    ^cmd1.sc:1: type arguments [cmd1.this.cmd0.Lion] do not conform to class PetContainer's type parameter bounds [P <: Helper.this.Pet]
val lionContainer = new PetContainer[Lion](new Lion)
                        ^cmd1.sc:8: type arguments [cmd1.this.cmd0.Lion] do not conform to class PetContainer's type parameter bounds [P <: Helper.this.Pet]
            .print(lionContainer, "lionContainer", _root_.scala.None)
             ^cmd1.sc:8: type arguments [cmd1.this.cmd0.Lion] do not conform to class PetContainer's type parameter bounds [P <: Helper.this.Pet]
            .print(lionContainer, "lionContainer", _root_.scala.None)
                  ^Compilation Failed
Compilation Failed

The class PetContainer take a type parameter P which must be a subtype of Pet. Dog and Cat are subtypes of Pet so we can create a new PetContainer[Dog] and PetContainer[Cat]. However, if we tried to create a PetContainer[Lion], we would get the following Error:

type arguments [Lion] do not conform to class PetContainer's type parameter bounds [P <: Pet]

This is because Lion is not a subtype of Pet.

Previous

Next

Tour of Scala