JuliaTesting / Aqua.jl

Auto QUality Assurance for Julia packages
MIT License
334 stars 24 forks source link

Type piracy of complex conversion of UniformScaling #261

Closed andreasvarga closed 7 months ago

andreasvarga commented 7 months ago

I am trying to define in my package MatrixEquations a simple conversion to complex type of an arbirary UniformScaling. For my first trial with

Base.complex(A::UniformScaling) = complex(A.λ)*I

Aqua complains of possible type piracy. The same occurs for two other definitions

Base.complex(A::UniformScaling{T}) where {T<:Real} = Complex{T}(A.λ)*I

Base.complex(A::UniformScaling) = Complex{eltype(A)}(A.λ)*I

I wonder what is the cause of this, because all three definitions work well for my purposes.

hyrodium commented 7 months ago

If you would like to avoid type piracies, your package needs to own function or argument type. The definition Base.complex(A::UniformScaling) = complex(A.λ)*I looks correct, so that should be defined in LinearAlgebra.jl.

The following articles would be useful to learn type piracies.

lgoettgens commented 7 months ago

Thanks for the explanation @hyrodium!

andreasvarga commented 7 months ago

Thanks for the explanation. I fixed the type piracy issue by defining my own function tocomplex as follows:

tocomplex(A::LinearAlgebra.UniformScaling) = complex(A.λ)*I
tocomplex(A::AbstractArray) = complex(A)
tocomplex(A::Number) = complex(A)