# Type stability question

**URL:** <https://discourse.julialang.org/t/type-stability-question/9060>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 14, 2018, 3:50pm UTC](https://discourse.julialang.org/t/type-stability-question/9060 "2018-02-14T15:50:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jianghaizhu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghaizhu/32/1866_2.png) [@jianghaizhu](https://discourse.julialang.org/u/jianghaizhu)\
**Post date:** [February 14, 2018, 3:50pm UTC](https://discourse.julialang.org/t/type-stability-question/9060/1 "2018-02-14T15:50:54Z")

</div>

I don’t quite understand the type instability in the following MWE.

```nohighlight
julia> function foo(A::AbstractArray{T, 2}) where T
           w = Int(sqrt(size(A, 1)))
           n = size(A, 3)
           A = reshape(A, w, w, n)
       end
foo (generic function with 1 method)

julia> x = rand(100, 4);

julia> @code_warntype foo(x)
Variables:
  #self# <optimized out>
  A@_2::Array{Float64,2}
  w::Int64
  n::Int64
  A@_5::Any

Body:
  begin 
      A@_5::Any = A@_2::Array{Float64,2}
      SSAValue(1) = (Base.arraysize)(A@_5::Array{Float64,2}, 1)::Int64
      SSAValue(2) = (Base.Math.sqrt_llvm)((Base.sitofp)(Float64, SSAValue(1))::Float64)::Float64
      w::Int64 = $(Expr(:invoke, MethodInstance for convert(::Type{Int64}, ::Float64), :(Base.convert), Int64, SSAValue(2))) # line 3:
      n::Int64 = (Base.arraysize)(A@_5::Array{Float64,2}, 3)::Int64 # line 4:
      SSAValue(0) = $(Expr(:invoke, MethodInstance for reshape(::Array{Float64,2}, ::Tuple{Int64,Int64,Int64}), :(Base.reshape), :(A@_5::Array{Float64,2}), :((Core.tuple)(w, w, n)::Tuple{Int64,Int64,Int64})))
      A@_5::Any = SSAValue(0)
      return SSAValue(0)
  end::Array{Float64,3}

```

Why the type of `A` is `Any`? Thanks!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 14, 2018, 3:55pm UTC](https://discourse.julialang.org/t/type-stability-question/9060/2 "2018-02-14T15:55:26Z")

</div>

This type instability is nothing to worry about. Anywhere `A@_5` is used it is inferred. Also, in 0.7 the list is:

```julia
julia> @code_warntype foo(x)
Variables:
  A@_2::Array{Float64,2}
  w<optimized out>
  n<optimized out>
  #temp#@_6::Bool
  #temp#@_7::Bool
  A@_9::Array{Float64,2}

```

---

<div class="post-metadata">

**Author:** ![jianghaizhu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghaizhu/32/1866_2.png) [@jianghaizhu](https://discourse.julialang.org/u/jianghaizhu)\
**Post date:** [February 14, 2018, 3:57pm UTC](https://discourse.julialang.org/t/type-stability-question/9060/3 "2018-02-14T15:57:42Z")

</div>

Thanks! This solved my question.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 14, 2018, 5:55pm UTC](https://discourse.julialang.org/t/type-stability-question/9060/4 "2018-02-14T17:55:55Z")

</div>

> [@jianghaizhu](#):
>
> `w = Int(sqrt(size(A, 1)))`

In general, it’s not a good idea to do this, since `Int(x)` will error if `x` is not exactly equal to an `Int`, e.g. `Int(sqrt(100))` will work, but `Int(sqrt(10))` will error. In your particular case, it’s not catastrophic, since the reshape operation will also fail exactly when the `Int()` call fails, but still.

It’s safer to use `isqrt(x)` or `round(Int, sqrt(x))` (or `floor`/`ceil`).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 14, 2018, 5:57pm UTC](https://discourse.julialang.org/t/type-stability-question/9060/5 "2018-02-14T17:57:31Z")

</div>

BTW. There’s something wrong with your code. This line

```julia
n = size(A, 3)

```

should be

```julia
n = size(A, 2)

```

I think.

---

<div class="post-metadata">

**Author:** ![jianghaizhu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghaizhu/32/1866_2.png) [@jianghaizhu](https://discourse.julialang.org/u/jianghaizhu)\
**Post date:** [February 14, 2018, 6:17pm UTC](https://discourse.julialang.org/t/type-stability-question/9060/6 "2018-02-14T18:17:37Z")

</div>

Yes. You are right. In my real code, I am sure it has an integer square root. But using isqrt() probably is a good idea.

---

<div class="post-metadata">

**Author:** ![jianghaizhu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghaizhu/32/1866_2.png) [@jianghaizhu](https://discourse.julialang.org/u/jianghaizhu)\
**Post date:** [February 14, 2018, 6:17pm UTC](https://discourse.julialang.org/t/type-stability-question/9060/7 "2018-02-14T18:17:58Z")

</div>

You are right. That was a typo. Thanks!
