About

Every variable and expression in a Scala program has a type that is known at compile time.

A type restricts the possible values to which a variable can refer, or an expression can produce, at run time.

A variable or expression’s type can also be referred to as:

  • a static type if necessary to differentiate it from
  • an object’s runtime type.

In other words, “type” by itself means static type.

Type is distinct from class because a class that takes type parameters can construct many types.

For example:

  • List is a class, but not a type.
  • List[T] is a type with a free type parameter.
  • List[Int] and List[String] are also types (called ground types because they have no free type parameters).

A type can have :

For example:

  • the class of type List[Int] is List.
  • the trait of type Set[String] is Set.

Type

Constraint

Some annotations are type constraints, meaning that they add additional limits, or constraints, on what values the type includes.

For example:

  • @positive could be a type constraint on the type Int, limiting the type of 32-bit integers down to those that are positive.

Type constraints are not checked by the standard Scala compiler, but must instead be checked by an extra tool or by a compiler plugin.

Constructor

A class or trait that takes type parameters.

Parameter

A parameter to a generic class or generic method that must be filled in by a type.

For example, the T in both cases is a type parameter.

  • class List is defined as class List[T] { . . .
  • Method Identity, a member of object Predef, is defined as def identity[T](x:T) = x.

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, Scala - Trait (Interface), or singleton object comprises:

  • its name,
  • the type signatures of all of its members and constructors,
  • and its declared inheritance and mixin relations.