# Why is dispatching on \`Val\` freezing Julia?

**URL:** <https://discourse.julialang.org/t/why-is-dispatching-on-val-freezing-julia/115267>\
**Category:** General Usage\
**Created:** [June 6, 2024, 5:08am UTC](https://discourse.julialang.org/t/why-is-dispatching-on-val-freezing-julia/115267 "2024-06-06T05:08:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [June 6, 2024, 5:08am UTC](https://discourse.julialang.org/t/why-is-dispatching-on-val-freezing-julia/115267/1 "2024-06-06T05:08:31Z")

</div>

I expected a `MethodError` but this freezes Julia.

```julia
f(::Val{0}) = nothing; f(x) = f(Val(x)); f(1)

```

```julia
julia> versioninfo()
Julia Version 1.10.3
Commit 0b4590a5507 (2024-04-30 10:59 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M1
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 6, 2024, 5:10am UTC](https://discourse.julialang.org/t/why-is-dispatching-on-val-freezing-julia/115267/2 "2024-06-06T05:10:30Z")

</div>

You get an infinite recursion. `Val(1)` creates the object `Val{1}()`, which then dispatches to your fallback method again, wrapping it in another `Val` (resulting in `Val{Val{1}()}()`) and so on. Nothing ever unwraps the `Val` & decreases `x`, so your `::Val{0}` method is never hit.

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [June 6, 2024, 5:26am UTC](https://discourse.julialang.org/t/why-is-dispatching-on-val-freezing-julia/115267/3 "2024-06-06T05:26:19Z")

</div>

Got it. I think I’d done something similar before and someone in the forum had helped me figure out what was going on. It’ll take me a few tries.
