# Something equivalent to assert or unsafe crash

**URL:** <https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454>\
**Category:** Learn\
**Created:** [April 9, 2019, 8:37am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454 "2019-04-09T08:37:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Mossey](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/michael_mossey/32/1995_2.png) [@Michael\_Mossey](https://discourse.elm-lang.org/u/Michael_Mossey)\
**Post date:** [April 9, 2019, 8:37am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/1 "2019-04-09T08:37:01Z")

</div>

In Haskell I frequently use “error” in development. first it’s a placeholder, able to take on any type, allowing me to see if my program type checks early on. Second, it’s kind of alike “asert” in that it will force a crash in an exceptional situation. I’d like to do something similar in elm. Is this possible? The two criteria are “something that takes on any type” and “something that crashes with an error message”

Mike

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [April 9, 2019, 8:44am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/2 "2019-04-09T08:44:56Z")

</div>

Hi Mike and welcome to the Elm community.

You can accomplish the same thing with `Debug.todo`

Here is the counter example with the decrement branch replaced by a `Debug.todo`:  
[https://ellie-app.com/5cSGhHc8zQWa1](https://ellie-app.com/5cSGhHc8zQWa1)

There is the added benefit that the compiler will not let you ship `Debug.todo` in production. If you compile with `--optimize` it will error if it sees any `Debug` usage.

---

<div class="post-metadata">

**Author:** ![gampleman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gampleman/32/43_2.png) [@gampleman](https://discourse.elm-lang.org/u/gampleman)\
**Post date:** [April 9, 2019, 8:59am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/3 "2019-04-09T08:59:55Z")

</div>

If you really need to, you can also write something like that for production usage, however I highly recommend against it.

```elm

crash : a -> b
crash arg =
  crash arg

```

---

<div class="post-metadata">

**Author:** ![zinggi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zinggi/32/62_2.png) [@zinggi](https://discourse.elm-lang.org/u/zinggi)\
**Post date:** [April 9, 2019, 6:44pm UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/4 "2019-04-09T18:44:24Z")

</div>

This is not good, as it gets translated to a while loop (since it’s tail recursive).  
If this code ever gets executed, the browser just freezes up.  
If you _really_ want a function that actually crashes in production, I’d use something like this:

```elm
crash : String -> a
crash msg =
    crash (crashHelp msg)

crashHelp : a -> a
crashHelp a =
    crashHelp (crashHelp a)

```

or this one, which only works because the compiler doesn’t properly optimize this yet:

```elm
crash : String -> a
crash x = x |> crash

```

But if you ever really want to actually use this in production, you should probably question your approach.

---

<div class="post-metadata">

**Author:** ![eike](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eike/32/749_2.png) [@eike](https://discourse.elm-lang.org/u/eike)\
**Post date:** [April 9, 2019, 7:33pm UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/5 "2019-04-09T19:33:48Z")

</div>

You can also incorporate one of the few edge cases where Elm still crashes. I know of two: Comparing functions that are not two references to the same function, for example `(\x -> x) == (\x -> x)`, or dividing by zero in the modulus function, i.e. `modBy 0 1`. Put one of these into a let binding in the `crash` function. (You still need to write some kind of recursion to trick the type system. But this way, it crashes on the first call instead of producing a stack overflow.)

---

<div class="post-metadata">

**Author:** ![Michael\_Mossey](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/michael_mossey/32/1995_2.png) [@Michael\_Mossey](https://discourse.elm-lang.org/u/Michael_Mossey)\
**Post date:** [April 9, 2019, 9:39pm UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/6 "2019-04-09T21:39:56Z")

</div>

It’s not clear if all of your answers are proposing a crash **with a message**. That’s the critical part, getting the message out so I know what failed.

I think Debug.todo must give a custom message based on what I see, so that will be fine.

---

<div class="post-metadata">

**Author:** ![eike](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eike/32/749_2.png) [@eike](https://discourse.elm-lang.org/u/eike)\
**Post date:** [April 10, 2019, 5:09am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/7 "2019-04-10T05:09:58Z")

</div>

ˋDebug.crashˋ is the only way to get a message out with your crash, as far as I know. However, it can’t be used when compiling in production mode, so if you really want something like it in production, the message-less versions above are the only way.

---

<div class="post-metadata">

**Author:** ![Y0hy0h](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/y0hy0h/32/3579_2.png) [@Y0hy0h](https://discourse.elm-lang.org/u/Y0hy0h)\
**Post date:** [April 10, 2019, 5:26am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/8 "2019-04-10T05:26:57Z")

</div>

I’m curious, could you explain your use case for the crash? I understand the “type prototyping”, but why do you want it to crash?

---

<div class="post-metadata">

**Author:** ![eike](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eike/32/749_2.png) [@eike](https://discourse.elm-lang.org/u/eike)\
**Post date:** [April 10, 2019, 7:27am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/9 "2019-04-10T07:27:26Z")

</div>

You don’t want to crash, of course. But occasionally, Elm’s type system is not strong enough to make every impossible state unrepresentable. In that case, you might end up in a situation where (you think) you know that a certain case cannot come up, but you can’t express it in the types and therefore you have to handle it. I think there are three types of solutions:

- You might return a `Maybe` and let the case bubble up to a place where you can handle it. But this is quite a heavy solution if the case is actually impossible.
- You can decide to return some default value. If the case is impossible it doesn’t matter what you return. But if there is an error in your reasoning and the case is possible after all, instead of just crashing you’re now corrupting your data with this incorrect default value.
- You can crash. I would argue that this is the best solution for actually impossible cases (because I’d rather crash than corrupt the data), but Elm does not provide a standard solution for that in production code. I guess the main reason is that one tends to get lazy and use this function even in cases where a better solution (for example a remodeling of the types to make the impossible state unrepresentable) exists.

By the way, Elm takes different approaches in the standard library: A lot of functions return `Maybe`s. Division by zero returns a default value (namely zero). Modulo by zero crashes. (I really don’t understand why these two are inconsistent, if division by zero returns zero, modulo by zero should return the number itself.)

---

<div class="post-metadata">

**Author:** ![Erkal\_Selman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/erkal_selman/32/269_2.png) [@Erkal\_Selman](https://discourse.elm-lang.org/u/Erkal_Selman)\
**Post date:** [April 10, 2019, 9:08am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/10 "2019-04-10T09:08:55Z")

</div>

> It’s not clear if all of your answers are proposing a crash **with a message**. That’s the critical part, getting the message out so I know what failed.

I would combine then one of the solutions above with [Debug.log](https://package.elm-lang.org/packages/elm/core/latest/Debug#log).

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [April 10, 2019, 9:11am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/11 "2019-04-10T09:11:39Z")

</div>

> [@Erkal\_Selman](#):
>
> I would combine then one of the solutions above with [Debug.log](https://package.elm-lang.org/packages/elm/core/latest/Debug#log).

But `Debug.log` is also not be allowed with `--optimize` ☹

---

<div class="post-metadata">

**Author:** ![gampleman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gampleman/32/43_2.png) [@gampleman](https://discourse.elm-lang.org/u/gampleman)\
**Post date:** [April 10, 2019, 9:44am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/12 "2019-04-10T09:44:33Z")

</div>

In production, knowing what crashes shouldn’t be necessary, since your crash should never happen there (and if it does, it will happen to someone else, so you won’t know anyway). In development, use `Debug.todo`.

---

<div class="post-metadata">

**Author:** ![Erkal\_Selman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/erkal_selman/32/269_2.png) [@Erkal\_Selman](https://discourse.elm-lang.org/u/Erkal_Selman)\
**Post date:** [April 10, 2019, 9:48am UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/13 "2019-04-10T09:48:14Z")

</div>

> But `Debug.log` is also not be allowed with `--optimize`☹

oh yes, sorry. My answer doesn’t make sense then.

---

<div class="post-metadata">

**Author:** ![Michael\_Mossey](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/michael_mossey/32/1995_2.png) [@Michael\_Mossey](https://discourse.elm-lang.org/u/Michael_Mossey)\
**Post date:** [April 10, 2019, 5:08pm UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/14 "2019-04-10T17:08:07Z")

</div>

I wasn’t clear - I don’t want this for production code, only for things in development.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [April 20, 2019, 5:22pm UTC](https://discourse.elm-lang.org/t/something-equivalent-to-assert-or-unsafe-crash/3454/15 "2019-04-20T17:22:51Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
