“No runtime errors” is a very high priority for Elm. This means that you are not allowed to create functions that can fail. (Unless you’re implementing a core function like modBy 0
.) The unsatisfying the solution of choosing a default value is probably your best choice. What I would do is:
- Use
Debug.todo
in your code while developing to mark the impossible branches. - Write exhaustive tests and make sure you never run into those errors.
- Replace
Debug.todo
by a default value.
(You might consider writing a custom wrapper around Debug.todo
that already takes a value of the correct type so that you only need to change the definition of your custom wrapper before finalizing the module instead of replacing every occurrence of Debug.todo
.)
Unfortunately, there are still two problems:
- Having to use different code for development and production builds is annoying.
- If you are tasked with producing a value of an unknown type (say you have a function
MyTree a -> a
and you know that the tree will always contain a value to return but you can’t prove it to the compiler), you’re out of luck and your “infinite recursion” solution is basically your only choice.