@@ -248,11 +248,16 @@ This merits more investigation but need not hold up the RFC.
248
248
# How We Teach This
249
249
[ how-we-teach-this ] : #how-we-teach-this
250
250
251
+ ### Where and how to document it
252
+
251
253
This RFC proposes extending an existing operator to permit the same
252
- general short-circuiting pattern to be used with more types. This more
253
- general behavior can be taught at the same time as the ` ? ` operator is
254
- introduced more generally, as part of teaching how best to do error
255
- handling in Rust.
254
+ general short-circuiting pattern to be used with more types. When
255
+ initially teaching the ` ? ` operator, it would probably be best to
256
+ stick to examples around ` Result ` , so as to avoid confusing the
257
+ issue. However, at that time we can also mention that ` ? ` can be
258
+ overloaded and offer a link to more comprehensive documentation, which
259
+ would show how ` ? ` can be applied to ` Option ` and then explain the
260
+ desugaring and how one goes about implementing one's own impls.
256
261
257
262
The reference will have to be updated to include the new trait,
258
263
naturally. The Rust book and Rust by example should be expanded to
@@ -264,6 +269,33 @@ impl and when it is not (i.e., expand on the concept of semantic
264
269
equivalence used to justify why we do not permit ` Option ` to be
265
270
interconverted with ` Result ` and so forth).
266
271
272
+ ### Error messages
273
+
274
+ Another important factor is the error message when ` ? ` is used in a
275
+ function whose return type is not suitable. The current error message
276
+ in this scenario is quite opaque and directly references the ` Carrer `
277
+ trait. A better message would be contingent on whether ` ? ` is applied
278
+ to a value that implements the ` Try ` trait (for any return type). If
279
+ not, we can give a message like
280
+
281
+ > ` ? ` cannot be applied to a value of type ` Foo `
282
+
283
+ If, however, ` ? ` * can* be applied to this type, but not with a function
284
+ of the given return type, we can instead report something like this:
285
+
286
+ > cannot use the ` ? ` operator in a function that returns ` () `
287
+
288
+ or perhaps if we want to be more strictly correct:
289
+
290
+ > ` ? ` cannot be applied to a ` io::Result ` in a function that returns ` () `
291
+
292
+ We could also go further and analyze the type to which ` ? ` is applied
293
+ and figure out the set of legal return types for the function to
294
+ have. So if the code is invoking ` foo.write()? ` (i.e., applying ` ? ` to an
295
+ ` io::Result ` ), then we could offer a suggestion like "consider changing
296
+ the return type to ` Result<(), io::Error> ` " or perhaps just "consider
297
+ changing the return type to a ` Result" ` .
298
+
267
299
# Drawbacks
268
300
[ drawbacks ] : #drawbacks
269
301
0 commit comments