Dotty Documentation


Context Bounds

Edit this page on GitHub

Context Bounds

A context bound is a shorthand for expressing a common pattern of an inferable parameter that depends on a type parameter. Using a context bound, the maximum function of the last section can be written like this:

def maximum[T: Ord](xs: List[T]): T = xs.reduceLeft(max)

A bound like : Ord on a type parameter T of a method or class indicates an inferable parameter given Ord[T]. The inferable parameter(s) generated from context bounds come last in the definition of the containing method or class. E.g.,

def f[T: C1 : C2, U: C3](x: T) given (y: U, z: V): R

would expand to

def f[T, U](x: T) given (y: U, z: V) given C1[T], C2[T], C3[U]: R

Context bounds can be combined with subtype bounds. If both are present, subtype bounds come first, e.g.

def g[T <: B : C](x: T): R = ...


TypeParamBounds   ::=  [SubtypeBounds] {ContextBound}
ContextBound      ::=  ‘:’ Type