# Why is this type inference failed

**URL:** https://discourse.julialang.org/t/why-is-this-type-inference-failed/60077
**Category:** General Usage
**Created:** [April 27, 2021, 2:42am UTC](https://discourse.julialang.org/t/why-is-this-type-inference-failed/60077 "2021-04-27T02:42:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![N5N3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n5n3/32/17663_2.png) [@N5N3](https://discourse.julialang.org/u/N5N3)
#### Post date: [April 27, 2021, 2:42am UTC](https://discourse.julialang.org/t/why-is-this-type-inference-failed/60077/1 "2021-04-27T02:42:18Z")

</div>

```julia
function interfer_test(
    obj::T,
    Ls::NTuple{2,<:Integer},
) where {T}
    CT = complex(T)
    mem_init = ntuple(_ -> zeros(CT, Ls), Val(4))
end
@code_warntype interfer_test(1,(100,100))

```

The above code returns

```julia
Variables
  #self#::Core.Const(interfer_test)
  obj::Int64
  Ls::Tuple{Int64, Int64}
  #33::var"#33#34"{Tuple{Int64, Int64}, DataType}
  mem_init::NTuple{4, Matrix{_A} where _A}
  CT::Type{Complex{Int64}}

Body::NTuple{4, Matrix{_A} where _A}
1 ─ (CT = Main.complex($(Expr(:static_parameter, 1))))
│ %2 = Main.:(var"#33#34")::Core.Const(var"#33#34")
│ %3 = Core.typeof(Ls)::Core.Const(Tuple{Int64, Int64})
│ %4 = Core.typeof(CT::Core.Const(Complex{Int64}))::Core.Const(DataType)
│ %5 = Core.apply_type(%2, %3, %4)::Core.Const(var"#33#34"{Tuple{Int64, Int64}, DataType})
│ (#33 = %new(%5, Ls, CT::Core.Const(Complex{Int64})))
│ %7 = #33::var"#33#34"{Tuple{Int64, Int64}, DataType}
│ %8 = Main.Val(4)::Core.Const(Val{4}())
│ %9 = Main.ntuple(%7, %8)::NTuple{4, Matrix{_A} where _A}
│ (mem_init = %9)
└── return %9

```

It seems the inferred `CT` wasn’t transmitted into the anonymous function.  
I tried this code on 1.5.1 ,1.6.1 and Master, and the results are similar.

---

<div class="post-metadata">

### Author: ![N5N3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n5n3/32/17663_2.png) [@N5N3](https://discourse.julialang.org/u/N5N3)
#### Post date: [April 27, 2021, 2:44am UTC](https://discourse.julialang.org/t/why-is-this-type-inference-failed/60077/2 "2021-04-27T02:44:23Z")

</div>

By the way, the following is ok

```julia
function interfer_test(obj::T, Ls::NTuple{2,<:Integer}) where {T}
    CT = complex(T)
    mem_init = ntuple(_ -> zeros(CT, (100,100)), Val(4))
end
@code_warntype interfer_test(1,(100,100))

```

which returns

```julia
Variables
  #self#::Core.Const(interfer_test)
  obj::Int64
  Ls::Tuple{Int64, Int64}
  #35::var"#35#36"{DataType}
  mem_init::NTuple{4, Matrix{Complex{Int64}}}
  CT::Type{Complex{Int64}}

Body::NTuple{4, Matrix{Complex{Int64}}}
1 ─ (CT = Main.complex($(Expr(:static_parameter, 1))))
│ %2 = Main.:(var"#35#36")::Core.Const(var"#35#36")
│ %3 = Core.typeof(CT::Core.Const(Complex{Int64}))::Core.Const(DataType)
│ %4 = Core.apply_type(%2, %3)::Core.Const(var"#35#36"{DataType})
│ (#35 = %new(%4, CT::Core.Const(Complex{Int64})))
│ %6 = #35::Core.Const(var"#35#36"{DataType}(Complex{Int64}))::Core.Const(var"#35#36"{DataType}(Complex{Int64}))
│ %7 = Main.Val(4)::Core.Const(Val{4}())
│ %8 = Main.ntuple(%6, %7)::NTuple{4, Matrix{Complex{Int64}}}
│ (mem_init = %8)
└── return %8

```
