# How to handle undefined

**URL:** <https://clash-lang.discourse.group/t/how-to-handle-undefined/105>\
**Category:** Clash Help\
**Created:** [March 15, 2026, 9:44pm UTC](https://clash-lang.discourse.group/t/how-to-handle-undefined/105 "2026-03-15T21:44:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flyingcog](https://avatars.discourse-cdn.com/v4/letter/f/9dc877/32.png) [@flyingcog](https://clash-lang.discourse.group/u/flyingcog)\
**Post date:** [March 15, 2026, 9:44pm UTC](https://clash-lang.discourse.group/t/how-to-handle-undefined/105/1 "2026-03-15T21:44:05Z")

</div>

I have a function that I would handle undefined in. Something like:

```auto
foo :: MyType -> State X ()
foo mt
    | isUndefined mt = do
        x .= undefined
        y .= undefined
foo (A a) = x .= a
foo (B b) = y .= b

```

However it generates a blackbox in the code because of catch method. How am I supposed to handle undefined?

---

<div class="post-metadata">

**Author:** ![flyingcog](https://avatars.discourse-cdn.com/v4/letter/f/9dc877/32.png) [@flyingcog](https://clash-lang.discourse.group/u/flyingcog)\
**Post date:** [March 15, 2026, 11:39pm UTC](https://clash-lang.discourse.group/t/how-to-handle-undefined/105/2 "2026-03-15T23:39:25Z")

</div>

Ok `clashSimulation && hasUndefined mt` seems to work

---

<div class="post-metadata">

**Author:** ![lmbollen](https://yyz2.discourse-cdn.com/free1/user_avatar/clash-lang.discourse.group/lmbollen/32/10_2.png) [@lmbollen](https://clash-lang.discourse.group/u/lmbollen)\
**Post date:** [March 16, 2026, 3:41pm UTC](https://clash-lang.discourse.group/t/how-to-handle-undefined/105/3 "2026-03-16T15:41:55Z")

</div>

It’s important to realize that checking if something is undefined is inherently a simulation time activity and will never actually work in hardware since there you will always have a value.

That being said, in Clash we typically use [errorX](https://hackage-content.haskell.org/package/clash-prelude-1.8.4/docs/Clash-XException.html#v:errorX) or [deepErrorX](https://hackage-content.haskell.org/package/clash-prelude-1.8.4/docs/Clash-XException.html#t:NFDataX) to create undefined expressions. These allow you to state the reason why that value is undefined. In your HDL they will also end up as undefined values which your synthesis tool can then “optimize away” by e.g. selecting an already existing valid value.

---

<div class="post-metadata">

**Author:** ![Rowan](https://yyz2.discourse-cdn.com/free1/user_avatar/clash-lang.discourse.group/rowan/32/24_2.png) [@Rowan](https://clash-lang.discourse.group/u/Rowan)\
**Post date:** [March 17, 2026, 12:45am UTC](https://clash-lang.discourse.group/t/how-to-handle-undefined/105/4 "2026-03-17T00:45:28Z")

</div>

In addition to what @lmbollen mentioned. Depending on what you actually want to achieve it might be more ergonomic to not set all fields `undefined/errorX`manually but rather use [`ensureSpine`](https://hackage-content.haskell.org/package/clash-prelude-1.8.4/docs/Clash-XException.html#v:ensureSpine) at the location where you expect the spine to exist. For example if you have `data X = X { x :: int, y :: int }` then `ensureSpine (errorX "Oops" :: X)` becomes `X { x = errorX "Oops" y = erroX "Oops" }`.

It’s not entirely clear if that is your goal from the original message, but it smells to me like that is precisely what you want to do here.

---

<div class="post-metadata">

**Author:** ![flyingcog](https://avatars.discourse-cdn.com/v4/letter/f/9dc877/32.png) [@flyingcog](https://clash-lang.discourse.group/u/flyingcog)\
**Post date:** [March 17, 2026, 2:21am UTC](https://clash-lang.discourse.group/t/how-to-handle-undefined/105/5 "2026-03-17T02:21:29Z")

</div>

> It’s important to realize that checking if something is undefined is inherently a simulation time activity and will never actually work in hardware since there you will always have a value.

Yes. See that my solution involve checking if it is simulation.

In my case if `mt` is undefined `x` and `y` are not used (don’t care). Which made me realize I probably should refactor code to make it more Haskelly using ADT but in my defense I haven’t used haskell in long time.

> It’s not entirely clear if that is your goal from the original message, but it smells to me like that is precisely what you want to do here.

Yes. Except Clash use strict state so at point when I pattern match it’s already too late if I understand things correctly as it returns undefined on state level.
