Here is an example of two independent packages colliding when they overload the same method.
Package MyPackage
Module MyPackage
struct MyNumber <: Number
value
end
Base.promote_rule(::Type{MyNumber}, ::Type{S}) where {S<:Number} = MyNumber
Base.convert(::Type{MyNumber}, x::MyNumber) = MyNumber(x.value)
Base.convert(::Type{MyNumber}, x::S) where {S<:Number} = MyNumber(x)
import Base: +
+(x::MyNumber, y::MyNumber) = MyNumber(x.value+y.value)
end
Package YourPackage
Module YourPackage
struct YourNumber <: Number
value
end
Base.promote_rule(::Type{YourNumber}, ::Type{S}) where {S<:Number} = YourNumber
Base.convert(::Type{YourNumber}, x::YourNumber) = YourNumber(x.value)
Base.convert(::Type{YourNumber}, x::S) where {S<:Number} = YourNumber(x)
import Base: +
+(x::YourNumber, y::YourNumber) = YourNumber(x.value+y.value)
end
Innocent User
julia> using MyPackage
julia> using YourPackage
julia> MyNumber(1) + MyNumber(2)
MyNumber(3)
julia> MyNumber(4) + 5
MyNumber(9)
julia> YourNumber(3) + YourNumber(3)
YourNumber(6)
julia> YourNumber(7) + 1
YourNumber(8)
So far so good. But then
julia> YourNumber(5) + MyNumber(3)
StackOverflowError:
Would you consider this a case of type piracy?
If you think that this is not a big deal, watch Cassette.jl: Overdub your Julia code | Cambridge Julia Meetup (May 2018) - YouTube