# Type instablility

**URL:** <https://discourse.julialang.org/t/type-instablility/6582>\
**Category:** New to Julia\
**Tags:** question, type-stability\
**Created:** [October 20, 2017, 2:38pm UTC](https://discourse.julialang.org/t/type-instablility/6582 "2017-10-20T14:38:38Z")\
**Posts on this page:** 5\
**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:** [October 20, 2017, 2:38pm UTC](https://discourse.julialang.org/t/type-instablility/6582/1 "2017-10-20T14:38:38Z")

</div>

I am playing with the `CartesianIndex` and here is a simple function to set the elements to 0 outside a centered N-sphere in a N-dimensional array.

```julia
julia> function test!(A::AbstractArray{T}, r::Int) where {T <: Real}
         ctr = [(size(A, d)+1)/2 for d in 1:ndims(A)]
         R = CartesianRange(size(A))
         for I in R
           (sum(abs2, (I.I .- ctr)) > r^2) && (A[I] = zero(T))
         end
         nothing
       end
test! (generic function with 1 method)

julia> A = rand(5, 5)
5×5 Array{Float64,2}:
 0.493302 0.536014 0.392092 0.913987 0.379203
 0.059023 0.927097 0.859462 0.875942 0.832613
 0.217616 0.137665 0.0311417 0.591856 0.598981
 0.28981 0.670475 0.454813 0.60883 0.558579
 0.523627 0.109789 0.343885 0.782731 0.386419

julia> @code_warntype test!(A, 2)
Variables:
  #self#::#test!
  A::Array{Float64,2}
  r::Int64
  I@_4::CartesianIndex{2}
  #temp#@_5::CartesianIndex{2}
  #5@_6::##5#6{Array{Float64,2}}
  ctr::Array{Float64,1}
  R::CartesianRange{CartesianIndex{2}}
  #3::Base.IteratorsMD.##3#4
  #5@_10::Base.IteratorsMD.##5#6{Int64}
  #temp#@_11::CartesianIndex{2}
  newtail::Tuple{Int64}
  #temp#@_13::Tuple{Int64,Int64}
  T::Any
  shape::Tuple{Base.OneTo{Int64}}
  iter::CartesianRange{CartesianIndex{1}}
  newout::Base.OneTo{Int64}
  C::Array{Float64,1}
  keeps@_19::Tuple{Tuple{Bool},Tuple{Bool}}
  Idefaults@_20::Tuple{Tuple{Int64},Tuple{Int64}}
  #temp#@_21::Int64
  keeps@_22::Tuple{Tuple{Bool}}
  Idefaults@_23::Tuple{Tuple{Int64}}
  #temp#@_24::Int64
  keep@_25::Tuple{Bool}
  Idefault@_26::Tuple{Int64}
  #temp#@_27::Int64
  A1::Any
  keeps@_29::Tuple{}
  Idefaults@_30::Tuple{}
  #temp#@_31::Int64
  keep@_32::Tuple{Bool}
  Idefault@_33::Tuple{Int64}
  #temp#@_34::Int64
  ind1@_35::Base.OneTo{Int64}
  keep@_36::Tuple{}
  Idefault@_37::Tuple{}
  #temp#@_38::Int64
  ind1@_39::Base.OneTo{Int64}
  keep@_40::Tuple{}
  Idefault@_41::Tuple{}
  #temp#@_42::Int64
  I_1::CartesianIndex{1}
  I_2::CartesianIndex{1}
  val_1::Int64
  val_2::Float64
  result::Float64
  I@_48::CartesianIndex{1}
  i#660::Int64
  I@_50::CartesianIndex{1}
  n#659::Int64
  i#658::CartesianIndex{0}
  #temp#@_53::Bool
  r#657::CartesianRange{CartesianIndex{1}}
  A_1::Tuple{Int64,Int64}
  A_2::Any
  keep_1::Tuple{Bool}
  keep_2::Tuple{Bool}
  Idefault_1::Tuple{Int64}
  Idefault_2::Tuple{Int64}
  #temp#@_61::Any
  fx::Float64
  ......

```

How do I make this function type stable? Thanks!

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [October 21, 2017, 6:44am UTC](https://discourse.julialang.org/t/type-instablility/6582/2 "2017-10-21T06:44:38Z")

</div>

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

You can try a function barrier (introduce a local function taking ctr and A as input)

---

<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:** [October 21, 2017, 12:59pm UTC](https://discourse.julialang.org/t/type-instablility/6582/3 "2017-10-21T12:59:06Z")

</div>

Thanks for the reply! I am aware of using function barrier here. I want to know how to eliminate the type instability issue in this function altogether. Maybe not as easy as I expected.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 21, 2017, 2:37pm UTC](https://discourse.julialang.org/t/type-instablility/6582/4 "2017-10-21T14:37:30Z")

</div>

I actually think it’s already type stable. The `Any`s are only for variables that are optimized out.

See [https://github.com/JuliaLang/julia/pull/23280](https://github.com/JuliaLang/julia/pull/23280). On the latest nightly version of Julia, all of the `Any`s are replaced with `<optimized out>`.

---

<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:** [October 21, 2017, 3:27pm UTC](https://discourse.julialang.org/t/type-instablility/6582/5 "2017-10-21T15:27:29Z")

</div>

Thanks for pointing this out! I think you are right. This function is already type stable.
