Scala - Type

Card Puncher Data Processing

Scala - Type

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.





Discover More
Card Puncher Data Processing
Scala - Class

Classes in Scala are: very similar to classes in Java. are templates containing fields and methods. Like in Java, classes can be instantiated using the new construct, there can be many “instances”...
Card Puncher Data Processing
Scala - Function

See Higher order functions are functions that take a function as a parameter or return functions. Call by value: evaluates the function arguments before calling the function Call by name: evaluates...
Card Puncher Data Processing
Scala - Main (Top-Level Objects)

In Scala, the main or entry point method is defined in an object. An object can be made executable by either: adding extending the type App or by adding a method object extends the type...
Card Puncher Data Processing
Scala - Method

A function, which is defined as a member of some object, is called a method. See



Share this page:
Follow us:
Task Runner