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