on master help sign in
Add Debug.crash to help with developing large functions
96cd20aEvan Czaplicki
libraries/Debug.elm+29−2
@@ -2,10 +2,11 @@ module Debug where
2{-| This library is for investigating bugs or performance problems. It should
2{-| This library is for investigating bugs or performance problems. It should
3*not* be used in production code.
3*not* be used in production code.
4
4
5−@docs log
5+@docs log, crash
6-}
6-}
7
7
8import Native.Debug
8import Native.Debug
9+import Native.Error
9
10
10{-| Log a tagged value on the developer console, and then return the value.
11{-| Log a tagged value on the developer console, and then return the value.
11
12
@@ -16,4 +17,30 @@ Notice that `log` is not a pure function! It should *only* be used for
16investigating bugs or performance problems.
17investigating bugs or performance problems.
17-}
18-}
18log : String -> a -> a
19log : String -> a -> a
19−log = Native.Debug.log
20+log = Native.Debug.log
21+
22+{-| Crash the program with an error message. This is an uncatchable error,
23+intended for code that is soon-to-be-implemented. For example, if you are
24+working with a large ADT and have partially completed a case expression, it may
25+make sense to do this:
26+
27+ data Entity = Ship | Fish | Captain | Seagull
28+
29+ drawEntity entity =
30+ case entity of
31+ Ship -> ...
32+ Fish -> ...
33+ _ -> Debug.crash ("drawEntity not implemented for " ++ show entity ++ " yet!")
34+
35+Note that incomplete pattern matches are *very* bad practice! They are one of
36+the very few ways to crash an Elm program, and they are completely avoidable.
37+Production code should not have incomplete pattern matches!
38+
39+**Use this if** you want to do some testing while you are partway through
40+writing a function.
41+
42+**Do not use this if** you want to do some typical try-catch exception handling.
43+Use the `Maybe` or `Either` libraries instead.
44+-}
45+crash : String -> a
46+crash = Native.Error.raise