# Temporary solution from user side for type piracy between packages?

**URL:** <https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496>\
**Category:** General Usage\
**Tags:** type-piracy\
**Created:** [May 22, 2019, 11:32pm UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496 "2019-05-22T23:32:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [May 22, 2019, 11:32pm UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/1 "2019-05-22T23:32:25Z")

</div>

Is there a quick fix for a package introducing type piracy? Currently, importing `Images` clobbers `Distances` because it imports `ImageDistances` that overloads `evaluate` (see [avoid type piracy? · Issue #28 · JuliaImages/ImageDistances.jl · GitHub](https://github.com/JuliaImages/ImageDistances.jl/issues/28)). Is there a temporary fix as a user that I can do till this is figured out?

```julia
julia> using Distances

julia> evaluate(WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
31.622776601683793

julia> sqrt(1000)
31.622776601683793

julia> using Images

julia> evaluate(WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
ERROR: MethodError: Distances.result_type(::WeightedEuclidean{Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}) is ambiguous. Candidates:
  result_type(dist::PreMetric, ::AbstractArray{#s19,N} where N where #s19<:Union{Colorant{T1,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T1,N} where N, T1} where N, T1}, ::AbstractArray{#s17,N} where N where #s17<:Union{Colorant{T2,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T2,N} where N, T2} where N, T2}) where {T1<:Number, T2<:Number} in ImageDistances at /home/tamas/.julia/packages/ImageDistances/okX3M/src/generic.jl:83
  result_type(dist::Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W, ::AbstractArray{T1,N} where N, ::AbstractArray{T2,N} where N) where {T1, T2} in Distances at /home/tamas/.julia/packages/Distances/HOWRG/src/wmetrics.jl:46
Possible fix, define
  result_type(::Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W, ::AbstractArray{T1<:(Union{Colorant{T1,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T1,N} where N, T1} where N, T1} where T1<:Number),N} where N, ::AbstractArray{T2<:(Union{Colorant{T2,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T2,N} where N, T2} where N, T2} where T2<:Number),N} where N)
Stacktrace:
 [1] evaluate(::WeightedEuclidean{Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}) at /home/tamas/.julia/packages/Distances/HOWRG/src/wmetrics.jl:0
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 22, 2019, 11:44pm UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/2 "2019-05-22T23:44:53Z")

</div>

`import` instead?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 23, 2019, 12:01am UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/3 "2019-05-23T00:01:42Z")

</div>

You should still be able to do `Distances.evaluate(...)`, if that’s what you’re looking for.

---

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [May 23, 2019, 12:37am UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/4 "2019-05-23T00:37:07Z")

</div>

That gives the same error:

```julia
julia> Distances.evaluate(WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
ERROR: MethodError: Distances.result_type(::WeightedEuclidean{Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}) is ambiguous. Candidates:
  result_type(dist::PreMetric, ::AbstractArray{#s19,N} where N where #s19<:Union{Colorant{T1,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T1,N} where N, T1} where N, T1}, ::AbstractArray{#s17,N} where N where #s17<:Union{Colorant{T2,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T2,N} where N, T2} where N, T2}) where {T1<:Number, T2<:Number} in ImageDistances at /home/tamas/.julia/packages/ImageDistances/okX3M/src/generic.jl:83
  result_type(dist::Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W, ::AbstractArray{T1,N} where N, ::AbstractArray{T2,N} where N) where {T1, T2} in Distances at /home/tamas/.julia/packages/Distances/HOWRG/src/wmetrics.jl:46
Possible fix, define
  result_type(::Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W, ::AbstractArray{T1<:(Union{Colorant{T1,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T1,N} where N, T1} where N, T1} where T1<:Number),N} where N, ::AbstractArray{T2<:(Union{Colorant{T2,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T2,N} where N, T2} where N, T2} where T2<:Number),N} where N)
Stacktrace:
 [1] evaluate(::WeightedEuclidean{Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}) at /home/tamas/.julia/packages/Distances/HOWRG/src/wmetrics.jl:0
 [2] top-level scope at none:

```

---

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [May 23, 2019, 12:38am UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/5 "2019-05-23T00:38:41Z")

</div>

```julia
julia> import Distances

julia> evaluate(WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
ERROR: UndefVarError: WeightedEuclidean not defined
Stacktrace:
 [1] top-level scope at none:0

julia> evaluate(Distances.WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
ERROR: UndefVarError: evaluate not defined
Stacktrace:
 [1] top-level scope at none:0

julia> Distances.evaluate(Distances.WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
31.622776601683793

julia> import Images

julia> Distances.evaluate(Distances.WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
ERROR: MethodError: Distances.result_type(::Distances.WeightedEuclidean{Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}) is ambiguous. Candidates:
  result_type(dist::Distances.PreMetric, ::AbstractArray{#s19,N} where N where #s19<:Union{Colorant{T1,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T1,N} where N, T1} where N, T1}, ::AbstractArray{#s17,N} where N where #s17<:Union{Colorant{T2,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T2,N} where N, T2} where N, T2}) where {T1<:Number, T2<:Number} in ImageDistances at /home/tamas/.julia/packages/ImageDistances/okX3M/src/generic.jl:83
  result_type(dist::Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W, ::AbstractArray{T1,N} where N, ::AbstractArray{T2,N} where N) where {T1, T2} in Distances at /home/tamas/.julia/packages/Distances/HOWRG/src/wmetrics.jl:46
Possible fix, define
  result_type(::Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W, ::AbstractArray{T1<:(Union{Colorant{T1,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T1,N} where N, T1} where N, T1} where T1<:Number),N} where N, ::AbstractArray{T2<:(Union{Colorant{T2,N} where N, AbstractArray{#s12,N} where #s12<:Union{Colorant{T2,N} where N, T2} where N, T2} where T2<:Number),N} where N)
Stacktrace:
 [1] evaluate(::Distances.WeightedEuclidean{Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}) at /home/tamas/.julia/packages/Distances/HOWRG/src/wmetrics.jl:0
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 23, 2019, 1:05am UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/6 "2019-05-23T01:05:40Z")

</div>

The `import` solution cannot work. Either:

- use `Base.delete_method(@which result_type(...))` to delete the unwanted method
- Define the method that’s suggested in the MethodError: `result_type(...) = invoke(result_type, ...)`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 23, 2019, 1:09am UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/7 "2019-05-23T01:09:20Z")

</div>

I am actually confused to why `import` still overrides things

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 23, 2019, 2:10am UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/8 "2019-05-23T02:10:52Z")

</div>

There is no difference between `using` and `import` in this respect: both cause the module to be loaded, and loading the module causes the pirate method to be added. `using` has the additional effect of bringing the exported symbols into the current namespace, but it doesn’t change the way module loading happens.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 23, 2019, 9:15am UTC](https://discourse.julialang.org/t/temporary-solution-from-user-side-for-type-piracy-between-packages/24496/9 "2019-05-23T09:15:23Z")

</div>

Read the error message and copy-paste the signature of the method you want. Then write

```julia
julia> const _sig = Tuple{Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W, AbstractArray{T1,N} where N, AbstractArray{T2,N} where N} where {T1, T2}
Tuple{Union{WeightedCityblock{W}, WeightedEuclidean{W}, WeightedHamming{W}, WeightedSqEuclidean{W}, WeightedMinkowski{W,T} where T<:Real} where W,AbstractArray{T1,N} where N,AbstractArray{T2,N} where N} where T2 where T1

julia> Distances.result_type(d::WeightedEuclidean{Array{T,1}}, a::Array{T,1}, b::Array{T,1}) where T<:Number = invoke(Distances.result_type, _sig, d, a, b)

julia> evaluate(WeightedEuclidean([1,1,1000]), [1,2,3], [1,2,4])
31.622776601683793

```
