<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">Catch-all considered harmful?</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">=============================</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">
I have been thinking about a potential source of bugs from catch-all pattern matches on sum types and would like to know your thoughts.
<br class=""></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">
Motivation</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">----------</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">
Totality is usually a desirable property of a function and the catch-all can conveniently buy us totality. But at what price?
I have been indoctrinated that rigour goes above convenience (think along the lines of: "Once we indulge in the impurities of I/O, there is no redemption.")
I would like to evaluate the trade-offs between convenience for the programmer and a potential source of bugs.
My questions to the community—
1. Are there real world examples of bugs caused by catch-alls?
2. Do you think that a language extension that disallows catch-alls (and annotations to opt back in at pattern match sites or type declaration) could be useful for certain code bases?
3. If this is a potential problem, then can you think of any better solutions a compiler could provide (i.e. that don't rely on an IDE / structured editing) other than disallowing catch-alls?
Feel free to chip in with your 2p (or 2¢), but please only if you have any concrete experience (or compelling theoretical evidence).
<br class=""></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">
Example</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">-------</pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class="">
Consider the sum type:
data Answer = No | Yes
and the function:
foo : Answer -> String
foo Yes = "Woo-hoo!"
foo _ = "Bother."
Say we need to extend our sum type:
data Answer = No | Perhaps | Yes
However, we forget to handle the new case appropriately in `foo`. The compiler is happy, but at runtime `foo Perhaps` would evaluate to `"Bother."`—with potentially catastrophic consequences.
(Please imagine this happening in a large codebase with several contributors, no single one of whom knows the entire codebase.)</pre><div class=""><br class=""></div></body></html>