# Failure to optimize due to type recursion

**URL:** <https://discourse.julialang.org/t/failure-to-optimize-due-to-type-recursion/108568>\
**Category:** General Usage\
**Tags:** question, inference\
**Created:** [January 9, 2024, 3:06pm UTC](https://discourse.julialang.org/t/failure-to-optimize-due-to-type-recursion/108568 "2024-01-09T15:06:47Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 9, 2024, 3:06pm UTC](https://discourse.julialang.org/t/failure-to-optimize-due-to-type-recursion/108568/1 "2024-01-09T15:06:48Z")

</div>

I find that it is relatively easy to get non-inferred code where the `mapfoldl` family is involved, because their implementation has so many layers. Eg a simple example is

```julia
_length(c::Real) = 1
_length(c::Tuple) = mapfoldl(_length, +, c; init = 0)
_length(c::NamedTuple) = _length(values(c))
_length(c::AbstractArray) = sum(_length, c; init = 0)

x = (a = 1, b = [2.0, 3.0], d = (4, 5f0))
@code_warntype _length(x)

using JET
@report_opt _length(x)

```

My understanding is that [#48059](https://github.com/JuliaLang/julia/pull/48059) is meant to address this, but it is currently dormant.

Am I abusing the inference engine with simple code as above? Should I do something differently, eg use generated functions for the tuples?

Just to clarify, I am looking for general advice, the above is an MWE. I routinely write code that fails inference because of deeply nested `mapXXX` functions. I am wondering if I should give that up, or do it differently.

---

_[View the full topic](https://discourse.julialang.org/t/failure-to-optimize-due-to-type-recursion/108568)._
