# Type inference through an if statement on a type parameter

**URL:** <https://discourse.julialang.org/t/type-inference-through-an-if-statement-on-a-type-parameter/7488>\
**Category:** General Usage\
**Created:** [December 4, 2017, 1:48am UTC](https://discourse.julialang.org/t/type-inference-through-an-if-statement-on-a-type-parameter/7488 "2017-12-04T01:48:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 4, 2017, 1:48am UTC](https://discourse.julialang.org/t/type-inference-through-an-if-statement-on-a-type-parameter/7488/1 "2017-12-04T01:48:02Z")

</div>

I am trying to make a function that specializes on the type parameter and Julia seems unable to concretely infer the types. Here is a MWE:

```julia
function h(a::NTuple{dim,Int}) where {dim}
       b = dim == 2 ? 2. : "a"
       c = b*b
       return c
end
@code_warntype h((1,1)) # Infers `b` to be a Union{Float64, String}

```

I ended up using a generated function to work around this as follows:

```julia
@generated function m(a::NTuple{dim,Int}) where {dim}
      b = dim == 2 ? 2. : "a"
      c = b*b
      return :($c)
end
@code_warntype h((1,1,1)) # Works fine

```

Is this an expected behaviour?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [December 4, 2017, 2:32am UTC](https://discourse.julialang.org/t/type-inference-through-an-if-statement-on-a-type-parameter/7488/2 "2017-12-04T02:32:55Z")

</div>

Use `dim === 2` (THis will be fixed by #24362)

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 4, 2017, 2:35am UTC](https://discourse.julialang.org/t/type-inference-through-an-if-statement-on-a-type-parameter/7488/3 "2017-12-04T02:35:12Z")

</div>

Works on nightly, thanks!
