Backport from sid to buster
[hcoop/debian/mlton.git] / doc / guide / src / TypeConstructor.adoc
1 TypeConstructor
2 ===============
3
4 In <:StandardML:Standard ML>, a type constructor is a function from
5 types to types. Type constructors can be _nullary_, meaning that
6 they take no arguments, as in `char`, `int`, and `real`.
7 Type constructors can be _unary_, meaning that they take one
8 argument, as in `array`, `list`, and `vector`. A program
9 can define a new type constructor in two ways: a `type` definition
10 or a `datatype` declaration. User-defined type constructors can
11 can take any number of arguments.
12
13 [source,sml]
14 ----
15 datatype t = T of int * real (* 0 arguments *)
16 type 'a t = 'a * int (* 1 argument *)
17 datatype ('a, 'b) t = A | B of 'a * 'b (* 2 arguments *)
18 type ('a, 'b, 'c) t = 'a * ('b -> 'c) (* 3 arguments *)
19 ----
20
21 Here are the syntax rules for type constructor application.
22
23 * Type constructor application is written in postfix. So, one writes
24 `int list`, not `list int`.
25
26 * Unary type constructors drop the parens, so one writes
27 `int list`, not `(int) list`.
28
29 * Nullary type constructors drop the argument entirely, so one writes
30 `int`, not `() int`.
31
32 * N-ary type constructors use tuple notation; for example,
33 `(int, real) t`.
34
35 * Type constructor application associates to the left. So,
36 `int ref list` is the same as `(int ref) list`.