Published on The Scala Programming Language (http://www.scala-lang.org)


By admin
Created 2008-07-05, 22:06

A Tour of Scala: Upper Type Bounds

In Scala, type parameters [1] and abstract types [2] 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 <: Adeclares that type variable T refers to a subtype of type A.

Here is an example which relies on an upper type bound for the implementation of the polymorphic method [3] findSimilar:

trait Similar {
  def isSimilar(x: Any): Boolean
}
case class MyInt(x: Int) extends Similar {
  def isSimilar(m: Any): Boolean =
    m.isInstanceOf[MyInt] &&
    m.asInstanceOf[MyInt].x == x
}
object UpperBoundTest extends Application {
  def findSimilar[T <: Similar](e: T, xs: List[T]): Boolean =
    if (xs.isEmpty) false
    else if (e.isSimilar(xs.head)) true
    else findSimilar[T](e, xs.tail)
  val list: List[MyInt] = List(MyInt(1), MyInt(2), MyInt(3))
  println(findSimilar[MyInt](MyInt(4), list))
  println(findSimilar[MyInt](MyInt(2), list))
}

Without the upper type bound annotation it would not be possible to call method isSimilar in method findSimilar.

The usage of lower type bounds is discussed here [4]. 


Source URL (retrieved on 2012-11-05, 19:46): http://www.scala-lang.org/node/136

Links:
[1] http://www.scala-lang.org/node/113
[2] http://www.scala-lang.org/node/105
[3] http://www.scala-lang.org/node/121
[4] http://www.scala-lang.org/node/137