Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / GenerativeDatatype.adoc
CommitLineData
7f918cf1
CE
1GenerativeDatatype
2==================
3
4In <:StandardML:Standard ML>, datatype declarations are said to be
5_generative_, because each time a datatype declaration is evaluated,
6it yields a new type. Thus, any attempt to mix the types will lead to
7a type error at compile-time. The following program, which does not
8type check, demonstrates this.
9
10[source,sml]
11----
12functor F () =
13 struct
14 datatype t = T
15 end
16structure S1 = F ()
17structure S2 = F ()
18val _: S1.t -> S2.t = fn x => x
19----
20
21Generativity also means that two different datatype declarations
22define different types, even if they define identical constructors.
23The following program does not type check due to this.
24
25[source,sml]
26----
27datatype t = A | B
28val a1 = A
29datatype t = A | B
30val a2 = A
31val _ = if true then a1 else a2
32----
33
34== Also see ==
35
36 * <:GenerativeException:>