Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 192 lines (162 sloc) 6.282 kb
1513f5e @agarwal capitalize titles consistently
agarwal authored
1 <!-- ((! set title Common Error Messages !)) ((! set learn !)) -->
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
2
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
3 *Table of contents*
4
1513f5e @agarwal capitalize titles consistently
agarwal authored
5 # Common Error Messages
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
6 This page gives a list of quick explanations for some error or warning
7 messages that are emitted by the OCaml compilers. Longer explanations
8 are usually given in dedicated sections of this tutorial.
9
10 ## Type errors
11 ### This expression has type ... but is here used with type ...
12 When the type of an object is not compatible with the context in which
13 it is used, it is frequent to obtain this kind of message:
14
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
15 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
16 1 + 2.5;;
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
17 ```
18 "This expression has type *X* but is here used with type *Y*" means that
19 if the contents of the expression is isolated (2.5), its type is
20 inferred as *X* (float). But the context, i.e. everything which is
21 around (1 + ...) tells that the gap expects an expression of type *Y*
22 (int) which is not compatible with *X*.
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
23
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
24 More disturbing is the following message:
25
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
26 ```text
27 This expression has type my_type but is here used with type my_type
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
28 ```
29 This error happens often while testing some type definitions using the
30 toplevel. In OCaml, it is perfectly legal to define a type with a name
31 that is already taken by another type. Consider the following session:
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
32
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
33 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
34 type my_type = A | B;;
35 let a = A;;
36 type my_type = A | B;;
37 let b = B;;
38 a = b;;
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
39 ```
40 For the compiler, the second definition of my_type is totally
41 independent from the first definition. So we have defined two types
42 which have the same name. Since "a" was defined earlier, it belongs to
43 the first type while "b" belongs to the second type. In this example,
44 redefining "a" after the last definition of my_type solves the problem.
45 This kind of problem should not happen in real programs unless you use
46 the same name for the same type in the same module, which is highly
47 discouraged.
48
49 ### Warning: This optional argument cannot be erased
50 Functions with optional arguments must have at least one non-labelled
51 argument. For instance, this is not OK:
52
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
53 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
54 let f ?(x = 0) ?(y = 0) = print_int (x + y)
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
55 ```
56 The solution is simply to add one argument of type unit, like this:
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
57
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
58 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
59 let f ?(x = 0) ?(y = 0) () = print_int (x + y);;
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
60 ```
61 See the [Labels](labels.html "Labels") section for more details on
62 functions with labelled arguments.
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
63
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
64 ### The type of this expression... contains type variables that cannot be generalized
65 This happens in some cases when the full type of an object is not known
66 by the compiler when it reaches the end of the compilation unit (file)
67 but for some reason it cannot remain polymorphic. Example:
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
68
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
69 ```ocamltop
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
70 let x = ref None
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
71 ```
72 triggers the following message during the compilation:
73
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
74 ```text
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
75 The type of this expression, '_a option ref,
76 contains type variables that cannot be generalized
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
77 ```
78 Solution: help the compiler with a type annotation, like for instance:
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
79
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
80 ```ocamltop
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
81 let x : string option ref = ref None
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
82 ```
83 or:
84
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
85 ```ocamltop
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
86 let x = ref (None : string option)
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
87 ```
88 Data of type `'_a` may be allowed temporarily, for instance during a
89 toplevel session. It means that the given object has an unknown type,
90 but it cannot be any type: it is not polymorphic data. In the toplevel,
91 our example gives these results:
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
92
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
93 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
94 let x = ref None
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
95 ```
96 The compiler tells us that the type of x is not fully known yet. But by
97 using `x` later, the compiler can infer the type of `x`:
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
98
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
99 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
100 x := Some 0
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
101 ```
102 Now `x` has a known type:
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
103
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
104 ```ocamltop
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
105 x;;
106 ```
107 More details are given in the [OCaml
108 FAQ](http://caml.inria.fr/pub/old_caml_site/FAQ/FAQ_EXPERT-eng.html#variables_de_types_faibles "http://caml.inria.fr/pub/old_caml_site/FAQ/FAQ_EXPERT-eng.html#variables_de_types_faibles").
109
110 ## Pattern matching warnings and errors
111 ### This pattern is unused
112 This warning should be considered as an error, since there is no reason
113 to intentionally keep such code. It may happen when the programmer
114 introduced a catch-all pattern unintentionally such as in the following
115 situation:
116
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
117 ```ocamltop
118 let test_member x tup =
119 match tup with
120 | (y, _) | (_, y) when y = x -> true
121 | _ -> false;;
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
122 ```
123 Obviously, the programmer had a misconception of what OCaml's pattern
124 matching is about. Remember the following:
125
126 * the tree of cases is traversed linearly, from left to right. There
127 is *no backtracking* as in regexp matching.
128 * a guard ("when" clause) is not part of a pattern. It is simply a
129 condition which is evaluated at most once and is used as a last
130 resort to jump to the next match case.
131 * lowercase identifiers (bindings such as "y" above) are just names,
132 so they will always match.
133
134 In our example, it is now clear that only the first item of the pair
135 will ever be tested. This leads to the following results:
136
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
137 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
138 test_member 1 (1, 0);;
139 test_member 1 (0, 1);;
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
140 ```
141 ### This pattern-matching is not exhaustive
142 OCaml's pattern matching can check whether a set of patterns is
143 exhaustive or not, based on the *type* only. So in the following
144 example, the compiler doesn't know what range of ints the "mod" operator
145 would return:
146
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
147 ```ocamltop
148 let is_even x =
149 match x mod 2 with
150 | 0 -> true
151 | 1 | -1 -> false
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
152 ```
153 A short solution without pattern matching would be:
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
154
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
155 ```ocamltop
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
156 let is_even x = x mod 2 = 0
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
157 ```
158 In general, that kind of simplification is not possible and the best
159 solution is to add a catch-all case which should never be reached:
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
160
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
161 ```ocamltop
162 let is_even x =
163 match x mod 2 with
164 | 0 -> true
165 | 1 | -1 -> false
166 | _ -> assert false
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
167 ```
168 ## Problems recompiling valid programs
169 ### x.cmi is not a compiled interface
170 When recompiling some old program or compiling a program from an
171 external source that was not cleaned properly, it is possible to get
172 this error message:
173
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
174 ```text
6466797 @agarwal Added tutorials_common_error_messages.html. Copied by permission from oc...
agarwal authored
175 some_module.cmi is not a compiled interface
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
176 ```
177 It means that some_module.cmi is not valid according to the *current
178 version* of the OCaml compiler. Most of the time, removing the old
179 compiled files (*.cmi, *.cmo, *.cmx, ...) and recompiling is
180 sufficient to solve this problem.
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
181
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
182 ### Warning: Illegal backslash escape in string
183 Recent versions of OCaml warn you against unprotected backslashes in
184 strings since they should be doubled. Such a message may be displayed
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
185 when compiling an older program, and can be turned off with the `-w x`
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
186 option.
187
7abfd3f @Chris00 Remove all ```tryocaml
Chris00 authored
188 ```ocamltop
434c7bb @Chris00 Tutorial: syntax highlight "Common error messages".
Chris00 authored
189 "\e\n" (* bad practice *);;
190 "\\e\n" (* good practice *);;
384ee97 @pw374 (redesign) html->md: actual conversion
pw374 authored
191 ```
Something went wrong with that request. Please try again.