Compiler enforced complete case/cond use?

I just reviewed some code of mine, that uses the cond keyword, and noticed it was, in fact, a partial function. I forgot to handle the default, _, case.
The compiler, for its part, cleared my code just fine, and it worked just fine… until I passed in the unhandled case.

So, as the title says, is there a way to force the compiler to validate the completeness of my case/cond/multi-clause functions (a-la Haskell/OCaml/F#, to name a few)?
I don’t mind if it’s built-in, 3rd party library… I just want to make sure the next time I forget to handle the default case, the compiler will yell at me about it.

ADT seems like it could help:

At least for case matching on the alternatives of a union type.

1 Like

Well, due to the missing compiletime information about types, the compiler can’t know if you had handled all cases except when you use _ explicitely.

But, in elixir it is not only common, but also idiomatic to simply not handle unwanted cases and “let it (the process) crash”.

So, if you have cases that are not handled, but possible and (somewhat) expected or wanted, you need to cover them through your tests. Everything else should just crash unless you have a good reason not to, but then you also know that you need to use _.

So I am totally against such a feature, as it would make “let it crash” harder and more verbose.


Didn’t think about the connection between types, or lack of, and the compiler’s ability to know about function completeness. Hmmm…

Also, I’ve read about Erlang/Elixir’s philosophy of “let it crash” (due to the supervision and whatnot), but am not yet there in my Elixir studies.
I have plenty on my plate, I can tell.

Well, I guess it’s a good practice to remember to handle all cases, including default, even if the compiler will warn you about it, all the more so if it won’t.

1 Like