Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / MLBasisAnnotationExamples.adoc
CommitLineData
7f918cf1
CE
1MLBasisAnnotationExamples
2=========================
3
4Here are some example uses of <:MLBasisAnnotations:>.
5
6== Eliminate spurious warnings in automatically generated code ==
7
8Programs that automatically generate source code can often produce
9nonexhaustive patterns, relying on invariants of the generated code to
10ensure that the pattern matchings never fail. A programmer may wish
11to elide the nonexhaustive warnings from this code, in order that
12legitimate warnings are not missed in a flurry of false positives. To
13do so, the programmer simply annotates the generated code with the
14`nonexhaustiveBind ignore` and `nonexhaustiveMatch ignore`
15annotations:
16
17----
18local
19 $(GEN_ROOT)/gen-lib.mlb
20
21 ann
22 "nonexhaustiveBind ignore"
23 "nonexhaustiveMatch ignore"
24 in
25 foo.gen.sml
26 end
27in
28 signature FOO
29 structure Foo
30end
31----
32
33
34== Deliver a library ==
35
36Standard ML libraries can be delivered via `.mlb` files. Authors of
37such libraries should strive to be mindful of the ways in which
38programmers may choose to compile their programs. For example,
39although the defaults for `sequenceNonUnit` and `warnUnused` are
40`ignore` and `false`, periodically compiling with these annotations
41defaulted to `warn` and `true` can help uncover likely bugs. However,
42a programmer is unlikely to be interested in unused modules from an
43imported library, and the behavior of `sequenceNonUnit error` may be
44incompatible with some libraries. Hence, a library author may choose
45to deliver a library as follows:
46
47----
48ann
49 "nonexhaustiveBind warn" "nonexhaustiveMatch warn"
50 "redundantBind warn" "redundantMatch warn"
51 "sequenceNonUnit warn"
52 "warnUnused true" "forceUsed"
53in
54 local
55 file1.sml
56 ...
57 filen.sml
58 in
59 functor F1
60 ...
61 signature S1
62 ...
63 structure SN
64 ...
65 end
66end
67----
68
69The annotations `nonexhaustiveBind warn`, `redundantBind warn`,
70`nonexhaustiveMatch warn`, `redundantMatch warn`, and `sequenceNonUnit
71warn` have the obvious effect on elaboration. The annotations
72`warnUnused true` and `forceUsed` work in conjunction -- warning on
73any identifiers that do not contribute to the exported modules, and
74preventing warnings on exported modules that are not used in the
75remainder of the program. Many of the
76<:MLBasisAvailableLibraries:available libraries> are delivered with
77these annotations.