A type declaration binds a type variable to a type. There are three kinds of type declarations: aliases, imports, and datatypes.
|
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.
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.
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.
{- 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 {- NONRUNNABLE -}
{- 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" {- NONRUNNABLE -}
{- 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 {- OUTPUT:PERMUTABLE Node(Node(Empty(), false, Empty()), true, Empty()) Node(Node(Empty(), 0, Empty()), 1, Node(Empty(), 2, Empty())) -}
Related Reference Topics
Related Tutorial Sections