- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
A Tour of Scala: Generic Classes
Created by admin on 2008-07-05.
Updated: 2008-12-21, 01:04
Like in Java 5 (aka. JDK 1.5), Scala has built-in support for classes parameterized with types. Such generic classes are particularly useful for the development of collection classes.
Here is an example which demonstrates this:
class Stack[T] { var elems: List[T] = Nil def push(x: T) { elems = x :: elems } def top: T = elems.head def pop() { elems = elems.tail } }
Class Stack
models imperative (mutable) stacks of an arbitrary element type T
. The use of type parameters allows to check that only legal elements (that are of type T
) are pushed onto the stack. Similarly, with type parameters we can express that method top
will only yield elements of the given type.
Here are some usage examples:
object GenericsTest extends Application { val stack = new Stack[Int] stack.push(1) stack.push('a') println(stack.top) stack.pop() println(stack.top) }
The output of this program will be:
97 1
Note that subtyping of generic types is invariant. This means that if we have a stack of characters of typeStack[Char]
then it cannot be used as an integer stack of type Stack[Int]
. This would be unsound because it would enable us to enter true integers into the character stack. To conclude, Stack[T] is only a subtype of Stack[S] iff S = T. Since this can be quite restrictive, Scala offers a type parameter annotation mechanism to control the subtyping behavior of generic types.