# Type inference with AbstractMatrix

**URL:** <https://discourse.julialang.org/t/type-inference-with-abstractmatrix/29038>\
**Category:** Internals & Design\
**Created:** [September 22, 2019, 10:29am UTC](https://discourse.julialang.org/t/type-inference-with-abstractmatrix/29038 "2019-09-22T10:29:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [September 22, 2019, 10:29am UTC](https://discourse.julialang.org/t/type-inference-with-abstractmatrix/29038/1 "2019-09-22T10:29:14Z")

</div>

In the MixedModels package the bulk of the work fitting a model consists of updating a blocked Cholesky factor to new parameter values. The matrix being factored consists of a square set of blocks of possibly different types (e.g. `SparseMatrixCSC`, `Diagonal`, `Matrix`). All of the blocks have the same element type, usually `Float64`.

It appears that the way I am writing methods does not allow the compiler to infer the type of the return value from methods like

```julia
sqrtpwrss(m::LinearMixedModel{T}) where {T} = T(first(m.L.blocks[end, end]))

```

Applying `@code_warntype` returns

```julia
julia> @code_warntype MixedModels.sqrtpwrss(m1)
Variables
  #self#::Core.Compiler.Const(MixedModels.sqrtpwrss, false)
  m::LinearMixedModel{Float64}

Body::Any
1 ─ %1 = Base.getproperty(m, :L)::BlockArrays.BlockArray{Float64,2,R} where R<:AbstractArray{Float64,2}
│ %2 = Base.getproperty(%1, :blocks)::Array{R,2} where R<:AbstractArray{Float64,2}
│ %3 = Base.lastindex(%2, 1)::Int64
│ %4 = Base.lastindex(%2, 2)::Int64
│ %5 = Base.getindex(%2, %3, %4)::AbstractArray{Float64,2}
│ %6 = MixedModels.first(%5)::Any
│ %7 = ($(Expr(:static_parameter, 1)))(%6)::Any
└── return %7

```

Is there a way I could write this to aid the type inference to produce a concrete type? Functions like this are called a lot.

---

<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:** [September 22, 2019, 10:39am UTC](https://discourse.julialang.org/t/type-inference-with-abstractmatrix/29038/2 "2019-09-22T10:39:17Z")

</div>

> [@dmbates](#):
>
> Is there a way I could write this to aid the type inference to produce a concrete type? Functions like this are called a lot.

Not really, but are you sure this is a performance problem? It feels like most of the computationally expensive functions should be behind function barriers and does be specialized on the type that gets passed in anyway.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 22, 2019, 2:21pm UTC](https://discourse.julialang.org/t/type-inference-with-abstractmatrix/29038/3 "2019-09-22T14:21:57Z")

</div>

Wouldn’t just a `::T` be enough? Or do you want to avoid writing that manually?
