4.6. type: Declare Type

A type declaration binds a type variable to a type. There are three kinds of type declarations: aliases, imports, and datatypes.

4.6.1. Syntax

[29]DeclareType::= DeclareTypeAlias
[30]DeclareTypeAlias::= type TypeVariable TypeParameters? = Type  
[31]DeclareTypeImport::= import type TypeVariable = ClassName  

4.6.2. Type Alias

A type alias gives an existing type a new name, for the programmer's ease of use. There is no distinction between the alias and the aliased type; they can be used interchangeably. A type alias may not be recursive.

A type alias may have type parameters, in which case it defines a type operator which can be applied to other types.

4.6.3. Type Import

A type import gives a name to a type described by some external service. This allows new types to be added to Orc, in much the same way that the import site declaration declaration allows new sites to be added to Orc. Type imports are often used in conjunction with the import site, to give a name to the type of values produced by the site.

4.6.4. Datatype

A datatype declaration defines a new type, called a sum type, which is the type of all values produced by any of the declared constructors.

A datatype declaration may be polymorphic, introducing new type variables which may be used within the slots of the constructors. It may also be recursive; the type name itself can be used within the slots of the constructors. A datatype declaration is the only way to define type recursively in Orc.

The datatype declaration also assigns types to each of the constructors that it defines.

4.6.5. Examples

Aliasing Types
{- Define a three-dimensional vector type, and a polymorphic relation type  -}

type Vector = {. x :: Number, y :: Number, z :: Number .}
type Relation[R] = (R,R) => Boolean

Importing Types
{- Import the Cell type of write-once cells, and import Java's URI type -}

import type Cell = "orc.lib.state.types.CellType"
import type URI = "java.net.URI"

Binary Tree Type
   Define a polymorphic binary tree datatype,
   then construct a tree of integers and a
   tree of booleans.

type Tree[T] = Node(Tree[T], T, Tree[T]) | Empty()

val intTree =
  val left = Node(Empty(), 0, Empty())
  val right = Node(Empty(), 2, Empty())
  Node(left, 1, right)

val boolTree =
  val left = Node(Empty(), false, Empty())
  Node(left, true, Empty())

intTree | boolTree 

Node(Node(Empty(), false, Empty()), true, Empty())
Node(Node(Empty(), 0, Empty()), 1, Node(Empty(), 2, Empty()))

4.6.6. Related Links

Related Tutorial Sections