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.
- 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 :
Some annotations are type constraints, meaning that they add additional limits, or constraints, on what values the type includes.
- @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.
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.
A method’s type signature comprises:
- its name,
- the number,
- and types of its parameters, if any,
- and its result type.
- its name,
- the type signatures of all of its members and constructors,
- and its declared inheritance and mixin relations.