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