# Type stability with chained parametric types

**URL:** <https://discourse.julialang.org/t/type-stability-with-chained-parametric-types/13076>\
**Category:** General Usage\
**Tags:** parametric-types, type-stability\
**Created:** [August 9, 2018, 9:48am UTC](https://discourse.julialang.org/t/type-stability-with-chained-parametric-types/13076 "2018-08-09T09:48:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [August 9, 2018, 9:48am UTC](https://discourse.julialang.org/t/type-stability-with-chained-parametric-types/13076/1 "2018-08-09T09:48:50Z")

</div>

Hi there! I have defined a custom type which includes both a vector of matrices, and a vector of matrix pairs, and am trying to achieve type stability. Here is a simplified version of my code:

```julia
using LinearAlgebra, InteractiveUtils

struct MatrixPair{U<:AbstractMatrix{<:Real}}
    A::U
    B::U
end

struct Matrices{T<:Real, U<:AbstractMatrix{T}, V<:AbstractVector{U}, W<:AbstractVector{MatrixPair{U}}}
    scalar::T
    matrices::V
    matrix_pairs::W
end

function sum(c::T, m::Matrices{T}) where T
    m.matrices[1] + m.matrix_pairs[1].A + m.matrix_pairs[1].B .+ (c + m.scalar)
end

M(m) = fill(Float64(m), 2, 2)

mp1 = MatrixPair(M(3), M(4))
mp2 = MatrixPair(M(5), M(6))
mat = Matrices(1.5, [M(1)], [mp1, mp2])

@code_warntype sum(3.14, mat)
display(sum(3.14, mat))

```

This works well in Julia 0.7, but in 0.6 (which I have to use for this project) I get:

```julia
MethodError: no method matching Matrices(::Float64, ::Array{Array{Float64,2},1}, ::Array{MatrixPair{Array{Float64,2}},1})

```

From what I can tell, this is due to no field in `Matrices` having type `U`, and a workaround is to create such a field in the `Matrices` struct: `dummy::U`, but this seems silly. Is there a better way? Can the type definition above be simplified/improved while keeping the type stability? Thanks!

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [August 9, 2018, 5:39pm UTC](https://discourse.julialang.org/t/type-stability-with-chained-parametric-types/13076/2 "2018-08-09T17:39:45Z")

</div>

I got your code to work in 0.6.2 by defining a method that explicitly figures out the type parameters and does the construction. Then I replaced the constructor call in your code with a call to this method:

```julia
matrices(A, B, C) = Matrices{typeof(A), eltype(typeof(B)), typeof(B), typeof(C)}(A,B,C)

```

I agree that this seems a bit unnecessary.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [August 10, 2018, 6:56am UTC](https://discourse.julialang.org/t/type-stability-with-chained-parametric-types/13076/3 "2018-08-10T06:56:37Z")

</div>

Ah, clever, that will work! I just changed it to an outer constructor `Matrices(A, B, C)`, so that when I migrate to 0.7 / 1.0 I can remove that constructor without changing the callers.
