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