# Type instability seemingly due to redundant inner functions that are never called

**URL:** https://discourse.julialang.org/t/type-instability-seemingly-due-to-redundant-inner-functions-that-are-never-called/133227
**Category:** General Usage
**Tags:** question, type-stability, closure, anonymous-function
**Created:** [October 17, 2025, 7:03am UTC](https://discourse.julialang.org/t/type-instability-seemingly-due-to-redundant-inner-functions-that-are-never-called/133227 "2025-10-17T07:03:17Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 17, 2025, 11:43am UTC](https://discourse.julialang.org/t/type-instability-seemingly-due-to-redundant-inner-functions-that-are-never-called/133227/2 "2025-10-17T11:43:23Z")

</div>

A rough answer is that closures are currently implemented at an early phase of compilation, called lowering, and lowering runs before inference and optimization. So lowering is mostly just based on syntax. I don’t understand the specific reason here, but basically lowering gives up here and boxes `k`, causing bad type inference for the later compilation phases (`k` is inferred as `Any`, the least precise type).

Seems curious that this happens even though `k` is never (re)assigned.

Some links:

- [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276)

- [State of closures, Fix1/Fix2](https://discourse.julialang.org/t/state-of-closures-fix1-fix2/118997)

---

_[View the full topic](https://discourse.julialang.org/t/type-instability-seemingly-due-to-redundant-inner-functions-that-are-never-called/133227)._
