Skip to content

Matrix * Vector uses convert with promote_op, causes issues on 1.1+ #632

Closed
JuliaLang/julia
#32097
@ararslan

Description

@ararslan

I don't have a minimal reproducing example at the moment, since this arose in private code, but consider the following situation: Say you have a Matrix{Float64} called A and a Vector{K} called x, where K is an abstract type. Various operations such as + and * are defined for K to return specific subtypes. The subtypes do not convert between each other. A * x should work; all of the necessary operations are defined.

With Julia 1.0, we get a Vector{Any} from A * x. Not ideal but fine. On 1.1, we get an error, saying it can't convert x's elements to a certain type. The issue seems to boil down to this difference:

On 1.0:

julia> Base.promote_op(LinearAlgebra.matprod, Float64, K)
Any

1.1:

julia> map(eltype, x)
3-element Array{DataType,1}:
 K0          
 K4
 K0   

julia> Base.promote_op(LinearAlgebra.matprod, Float64, K)
Union{K1, K2, K3}

LinearAlgebra then tries to do convert(AbstractArray{Union{K1, K2, K3}, x), which fails, since it can't convert x's elements to that type.

As of JuliaLang/julia#29739, promote_op is just a wrapper atop _return_type, which is returning the correct answer, but is not appropriate for this use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions