# Type Instability between 0.5 and 0.6 - Type Definition

**URL:** <https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837>\
**Category:** General Usage\
**Created:** [March 23, 2017, 4:57pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837 "2017-03-23T16:57:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pazzo83](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pazzo83/32/232_2.png) [@pazzo83](https://discourse.julialang.org/u/pazzo83)\
**Post date:** [March 23, 2017, 4:57pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837/1 "2017-03-23T16:57:25Z")

</div>

Hi all, I have begun to migrate my package from 0.5 to 0.6, and I’ve noticed something interesting regarding type instability between the two. Best to show with code:  
0.5

```julia
abstract ABC 

type A <: ABC 
a::Float64 
end 

type B <: ABC 
a::Float64 
end 

type C{T <: ABC} 
a::T 
end

```

Here’s the code\_warntype for:

```julia
@code_warntype C(A(4.5))
Variables:
   #self#::Type{C}
   a::A

Body: 
  begin 
        return $(Expr(:new, C{A}, :(a))) 
  end::C{A}

```

Vs 0.6:

```julia
abstract type ABC end 

struct A <: ABC 
a::Float64 
end 

struct B <: ABC 
a::Float64 
end 

struct C{T <: ABC} 
a::T 
end

```

And the code\_warntype:

```julia
Variables:
  #self#::Any 
  a::A

Body: 
   begin 
          return $(Expr(:new, :($(QuoteNode(C{A}))), :(a))) 
   end::C{A}

```

I’m not sure how material this might be in the end (it clearly can resolve the type at the very end), but I’ve noticed particular slow downs in my code between 0.5 and 0.6 where in 0.6 the compiler is unable to resolve certain types that it could in 0.5. Is there some other way we should be defining things?

Thanks!

Chris

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 23, 2017, 5:09pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837/2 "2017-03-23T17:09:37Z")

</div>

That `Any` is not used.

---

<div class="post-metadata">

**Author:** ![pazzo83](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pazzo83/32/232_2.png) [@pazzo83](https://discourse.julialang.org/u/pazzo83)\
**Post date:** [March 23, 2017, 6:22pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837/3 "2017-03-23T18:22:16Z")

</div>

OK, that makes sense since it was able to resolve the type in the end. I am however still seeing type instability that I don’t quite understand. I don’t see it in 0.5 and I actually don’t see it in 0.6 while still using “type” to define types (as opposed to struct/mutable struct). Here are the types in question:

```julia
mutable struct TermStructureFittingParameter{T <: TermStructure} <: Parameter
  times::Vector{Float64}
  values::Vector{Float64}
  ts::T
end

mutable struct HullWhiteDynamics{P<:Parameter} <: ShortRateDynamics{P}
  process::OrnsteinUhlenbeckProcess
  fitting::P
  a::Float64
  sigma::Float64
end

mutable struct OneFactorShortRateTree{S <: ShortRateDynamics, P <: StochasticProcess} <: ShortRateTree
  tree::TrinomialTree{P}
  dynamics::S
  tg::TimeGrid
  treeLattice::TreeLattice1D{OneFactorShortRateTree{S, P}}

  function OneFactorShortRateTree{S, P}(tree::TrinomialTree{P}, dynamics::S, tg::TimeGrid) where {S, P}
    oneFactorTree = new{S, P}(tree, dynamics, tg)
    oneFactorTree.treeLattice = TreeLattice1D(tg, get_size(tree, 2), oneFactorTree)

    return oneFactorTree
  end
end

```

The compiler in 0.6 with these definitions can’t seem to resolve the type of the HullWhiteDynamics in the Tree type (the last one defined). It has no problem in 0.5 or 0.6 using the “type” keyword. See here, this code\_warntype usage is calling a method with the tree passed in as an argument:

```julia
Variables:
  #self#::QuantLib.#get_state_prices!
  tree::QuantLib.OneFactorShortRateTree{QuantLib.HullWhiteDynamics,QuantLib.OrnsteinUhlenbeckProcess}
  i::Int64

```

vs:

````julia
Variables:
  #self#::QuantLib.#get_state_prices!
  tree::QuantLib.OneFactorShortRateTree{QuantLib.HullWhiteDynamics{QuantLib.TermStructureFittingParameter{QuantLib.FlatForwardTermStructure{QuantLib.T
ime.TargetCalendar,QuantLib.Time.Actual365,QuantLib.ContinuousCompounding,QuantLib.Time.Annual}}},QuantLib.OrnsteinUhlenbeckProcess}
  i::Int64```

As you can see in the latter output, it knows exactly what type of HullWhiteDynamics is passed in. Is there something wrong with the way I am using struct/mutable struct and some of the new syntax?

Thanks!

Chris
````

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 23, 2017, 6:37pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837/4 "2017-03-23T18:37:29Z")

</div>

Impossible to tell without full code.

---

<div class="post-metadata">

**Author:** ![pazzo83](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pazzo83/32/232_2.png) [@pazzo83](https://discourse.julialang.org/u/pazzo83)\
**Post date:** [March 23, 2017, 6:43pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837/5 "2017-03-23T18:43:01Z")

</div>

OK - it’s a bit involved, but the example driving this is here (from the “main” function):  
[https://github.com/pazzo83/QuantLib.jl/blob/julia0\_6/examples/swaption\_test\_code.jl](https://github.com/pazzo83/QuantLib.jl/blob/julia0_6/examples/swaption_test_code.jl)

The related code is here:  
[https://github.com/pazzo83/QuantLib.jl/blob/julia0\_6/src/termstructures/yield/yield\_term\_structure.jl](https://github.com/pazzo83/QuantLib.jl/blob/julia0_6/src/termstructures/yield/yield_term_structure.jl)  
[https://github.com/pazzo83/QuantLib.jl/blob/julia0\_6/src/models/parameter.jl](https://github.com/pazzo83/QuantLib.jl/blob/julia0_6/src/models/parameter.jl)  
[https://github.com/pazzo83/QuantLib.jl/blob/julia0\_6/src/models/short\_rate/one\_factor/hull\_white.jl](https://github.com/pazzo83/QuantLib.jl/blob/julia0_6/src/models/short_rate/one_factor/hull_white.jl)  
[https://github.com/pazzo83/QuantLib.jl/blob/julia0\_6/src/models/short\_rate/one\_factor/one\_factor.jl](https://github.com/pazzo83/QuantLib.jl/blob/julia0_6/src/models/short_rate/one_factor/one_factor.jl)

---

<div class="post-metadata">

**Author:** ![pazzo83](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pazzo83/32/232_2.png) [@pazzo83](https://discourse.julialang.org/u/pazzo83)\
**Post date:** [March 23, 2017, 6:55pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837/6 "2017-03-23T18:55:44Z")

</div>

Specifically, a method called “tree” gets called in hull\_white.jl (line 42), which is where this tree object is created:

```julia
numericTree = OneFactorShortRateTree{HullWhiteDynamics, typeof(numericDynamics.process)}(trinomial, numericDynamics, grid)

```

In 0.5, when I call this method, it’s able to resolve all the types associated with this obj:

````julia
 get_state_prices!(numericTree, i)```

In 0.6, it can't.
````

---

<div class="post-metadata">

**Author:** ![pazzo83](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pazzo83/32/232_2.png) [@pazzo83](https://discourse.julialang.org/u/pazzo83)\
**Post date:** [March 23, 2017, 7:48pm UTC](https://discourse.julialang.org/t/type-instability-between-0-5-and-0-6-type-definition/2837/7 "2017-03-23T19:48:28Z")

</div>

Just thought I’d update - it seems in 0.5 it was able to resolve just passing in HullWhiteDynamics as the type parameter, but not in 0.6. So I had to call typeof to pass in the exact type. Much better now.
