# Patching without adding dependencies

**URL:** <https://discourse.julialang.org/t/patching-without-adding-dependencies/35762>\
**Category:** General Usage\
**Created:** [March 9, 2020, 5:51pm UTC](https://discourse.julialang.org/t/patching-without-adding-dependencies/35762 "2020-03-09T17:51:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [March 9, 2020, 5:51pm UTC](https://discourse.julialang.org/t/patching-without-adding-dependencies/35762/1 "2020-03-09T17:51:48Z")

</div>

Let’s say I created some package `MyPackage` that I would like to interact with a package `TheirPackage` that I did not create. Is there any way to override some of the functions of `TheirPackage` without having to add `TheirPackage` to `MyPackage`’s dependencies? For example, if `MyPackage` has a type

```julia
struct MyVectorType{T} <: AbstractVector{T}
    arr::Vector{T}
end

Base.setindex!(x::MyVectorType, v, ind...) = setindex!(x.arr, v, ind...)
Base.getindex(x::MyVectorType, ind...) = getindex(x.arr, ind...)

Base.length(x::MyVectorType) = length(x.arr)
Base.size(x::MyVectorType) = size(x.arr)

```

and `TheirPackage` has a function that isn’t generic enough to take in any `AbstractVector` but probably should be

```julia
theirfunction(x::Vector) = x.^2

```

Is there a way to extend

```julia
TheirPackage.theirfunction(x::AbstractVector) = x.^2

```

or, I guess more safely

```julia
TheirPackage.theirfunction(x::MyVectorType) = x.^2

```

so if someone just happened to want to use `MyVectorType` with `TheirPackage` they could? Since the purpose of `MyPackage` is more general than just using it with `TheirPackage`, it doesn’t make sense to have it as a dependency. I guess this is something close to what recipes are for `Plots.jl`.

The only thing I can think of is defining a new package `MyPackagePatches` that can be imported for users who want to use `MyPackage` with packages that need patching, but that seems pretty convoluted.

---

<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:** [March 9, 2020, 6:14pm UTC](https://discourse.julialang.org/t/patching-without-adding-dependencies/35762/2 "2020-03-09T18:14:09Z")

</div>

You can do this with [Requires.jl](https://github.com/JuliaPackaging/Requires.jl) (you’re describing pretty much the textbook use case for that package). I would recommend your second option (defining `theirfunction` on `MyVectorType`) to avoid type piracy.

On the other hand, in the case that you specified it might be even more effective to simply open a PR to make `TheirPackage` more generic. That would help even more people with their own `AbstractVector` types.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [March 9, 2020, 6:32pm UTC](https://discourse.julialang.org/t/patching-without-adding-dependencies/35762/3 "2020-03-09T18:32:22Z")

</div>

That’s exactly what I was looking for, thanks. I submitted a PR for the package in question a little while back but it hasn’t been accepted yet so I figured I’d just patch it on my side for the time being.
