[Texas PLT logo]

COMP 402: Production Programming

  Scala Inheritance and the Scala Type System  

Scala Types

Definitions

Here are some definitions of terms we used in class today. The definitions are quoted from the glossary of Programming in Scala, 1st ed.

Currying
A way to write functions with multiple parameter lists. For instance def f(x: Int)(y: Int) is a curried function with two parameter lists. A curried function is applied by passing several arguments lists, as in: f(3)(4). However, it is also possible to write a partial application of a curried function, such as f(3).
Contravariant
A contravariant annotation can be applied to a type parameter of a class or trait by putting a minus sign (-) before the type parameter. The class or trait then subtypes contravariantly with-in the opposite direction as-the type annotated parameter. For example, Function1 is contravariant in its first type parameter, and so Function1[Any, Any] is a subtype of Function1[String, Any].
Covariant
A covariant annotation can be applied to a type parameter of a class or trait by putting a plus sign (+) before the type parameter. The class or trait then subtypes covariantly with-in the same direction as-the type annotated parameter. For example, List is covariant in its type parameter, so List[String] is a subtype of List[Any].
Type Parameter
A parameter to a generic class or generic method that must be filled in by a type. For example, class List is defined as "class List[T] { ...", and method identity, a member of object Predef, is defined as "def identity[T](x:T) = x". The T in both cases is a type parameter.
Type Signature
A method's type signature comprises its name, the number, order, and types of its parameters, if any, and its result type. The type signature of a class, trait, or singleton object comprises its name, the type signatures of all of its members and constructors, and its declared inheritance and mixin relations.

Covariance/Contravariance Example

Here is a nice example of covariance/contravariance from the Programming in Scala book:

http://www.artima.com/pins1ed/type-parameterization.html#lst:function-variance

  Scala Inheritance and the Scala Type System  

URL: http://www.cs.rice.edu/~javaplt/402/12-spring/lectures/scala4/index.shtml