Dave Doty
2017-08-20 20:12:58 UTC
I have found many situations in which I am forced by the compiler to deal
with a situation that will never come up at runtime (or, if it does come
up, it's a bug in my code and not something the user can do to fix). For
example, I might make a provably non-empty List (e.g., it is always made by
the :: operator) and later ask for its max value:
max: List Int -> Int
max list =
case List.maximum list of
Just v ->
v
Nothing ->
Debug.crash "This should be unreachable."
The "standard" way to handle this would be to make max return a Maybe or a
Result instead (i.e., just use List.maximum directly), but this can have
the effect of altering the type signature of several other functions, all
for an error that represents a bug in my program, not something caused by
user error that the user needs to be alerted about. This might be 10 levels
deep into a function call, and it seems dumb to change the type signature
of every function from max all the way back up to the top to return Maybe,
just to handle the situation (list is empty) that can only arise through
programmer error, not user error.
Is there an "standard" way of dealing with situations like this? I assume
it's not Debug.crash, given the ample warnings against using it in
production code. But despite the advertisements of "no runtime exceptions
in practice", sometimes it seems that there really is no graceful way to
handle programming errors other than to tell the user there's a bug in the
code, and dump a stack trace so that the bug can be tracked down. In other
words, a runtime exception seems like it does exactly what is needed in
this situation.
with a situation that will never come up at runtime (or, if it does come
up, it's a bug in my code and not something the user can do to fix). For
example, I might make a provably non-empty List (e.g., it is always made by
the :: operator) and later ask for its max value:
max: List Int -> Int
max list =
case List.maximum list of
Just v ->
v
Nothing ->
Debug.crash "This should be unreachable."
The "standard" way to handle this would be to make max return a Maybe or a
Result instead (i.e., just use List.maximum directly), but this can have
the effect of altering the type signature of several other functions, all
for an error that represents a bug in my program, not something caused by
user error that the user needs to be alerted about. This might be 10 levels
deep into a function call, and it seems dumb to change the type signature
of every function from max all the way back up to the top to return Maybe,
just to handle the situation (list is empty) that can only arise through
programmer error, not user error.
Is there an "standard" way of dealing with situations like this? I assume
it's not Debug.crash, given the ample warnings against using it in
production code. But despite the advertisements of "no runtime exceptions
in practice", sometimes it seems that there really is no graceful way to
handle programming errors other than to tell the user there's a bug in the
code, and dump a stack trace so that the bug can be tracked down. In other
words, a runtime exception seems like it does exactly what is needed in
this situation.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.