# Unexpected result when promoting Diagonal

**URL:** <https://discourse.julialang.org/t/unexpected-result-when-promoting-diagonal/85832>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 16, 2022, 3:49pm UTC](https://discourse.julialang.org/t/unexpected-result-when-promoting-diagonal/85832 "2022-08-16T15:49:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ViktorSvensson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viktorsvensson/32/38822_2.png) [@ViktorSvensson](https://discourse.julialang.org/u/ViktorSvensson)\
**Post date:** [August 16, 2022, 3:49pm UTC](https://discourse.julialang.org/t/unexpected-result-when-promoting-diagonal/85832/1 "2022-08-16T15:49:14Z")

</div>

When promoting Diagonals with different element types, I’m encountering unexpected behavior:

```julia
using LinearAlgebra
diagInt = Diagonal([1])
diagFloat = Diagonal([1.0])
promote_type(typeof(diagInt),typeof(diagFloat)) #returns Diagonal

```

Instead of Diagonal, I expected Diagonal{Float64, Vector{Float64}}. The promotion should be handled by [this promote\_rule](https://github.com/JuliaLang/julia/blob/ec98087cbf19bac26f6be05df9282746b0cffe78/stdlib/LinearAlgebra/src/diagonal.jl#L17) in the source code, but it seems that it is never called. In fact, if I copy and paste the method into the REPL, the promotion works as expected:

```julia
function Base.promote_rule(A::Type{<:Diagonal{<:Any,V}}, B::Type{<:Diagonal{<:Any,W}}) where {V,W}
    X = promote_type(V, W)
    T = eltype(X)
    isconcretetype(T) && return Diagonal{T,X}
    return typejoin(A, B)
end
promote_type(typeof(diagInt),typeof(diagFloat)) #now returns Diagonal{Float64, Vector{Float64}}

```

Why would overwriting the method with itself change the result?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 16, 2022, 6:54pm UTC](https://discourse.julialang.org/t/unexpected-result-when-promoting-diagonal/85832/2 "2022-08-16T18:54:23Z")

</div>

Hmmm. I do get `Diagonal{Float64, Vector{Float64}}` on Julia 1.8, but on 1.7 and below it’s `Diagonal` as you noted.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 16, 2022, 7:00pm UTC](https://discourse.julialang.org/t/unexpected-result-when-promoting-diagonal/85832/3 "2022-08-16T19:00:52Z")

</div>

The only difference afaict is that `@_inline_meta` in the older versions is replaced by `@inline` in Julia 1.8, within `promote_type`s body.

---

<div class="post-metadata">

**Author:** ![ViktorSvensson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viktorsvensson/32/38822_2.png) [@ViktorSvensson](https://discourse.julialang.org/u/ViktorSvensson)\
**Post date:** [August 17, 2022, 6:56am UTC](https://discourse.julialang.org/t/unexpected-result-when-promoting-diagonal/85832/4 "2022-08-17T06:56:12Z")

</div>

Thanks for checking. The answer is indeed that the method is not present on versions earlier than 1.8, and I’m using 1.7.3. It was added in [this commit](https://github.com/JuliaLang/julia/pull/42142).
