Type promotion clarification

Hi All,

Complete Julia newbie here. I thought I roughly understood the idea behind the promote_rule and convert stuff (after looking at the docs and a few articles) but I don’t understand the following

abstract type A end
struct A1 <: A val1::Float64 end
struct A2 <: A val2::Float64 end
promote_rule(::Type{A2}, ::Type{A1}) = A1
promote_rule(::Type{A1}, ::Type{A2}) = A1  # AFAIU this should not actually be needed
promote_type(A1, A2)  # I expect this to return A1 but it returns A

Can anyone explain why the call to promote_type returns A not A1? (I’m using 1.5.2 if it matters)

Thanks,
Dan

do: Base.promote_rule(::Type{A1}, ::Type{A2}) = A1

Then

julia> promote_type(A1, A2)
A1
1 Like

Ah! Yes that works, thanks.