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