# Manually Merge Functions

**URL:** <https://discourse.julialang.org/t/manually-merge-functions/59713>\
**Category:** New to Julia\
**Created:** [April 21, 2021, 6:33am UTC](https://discourse.julialang.org/t/manually-merge-functions/59713 "2021-04-21T06:33:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andenrx](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@andenrx](https://discourse.julialang.org/u/andenrx)\
**Post date:** [April 21, 2021, 6:33am UTC](https://discourse.julialang.org/t/manually-merge-functions/59713/1 "2021-04-21T06:33:29Z")

</div>

I have seen a [lot of discussion related to this](https://discourse.julialang.org/t/function-name-conflict-adl-function-merging/10335) and the general consensus seems to be that it would be a mistake to automatically merge functions from different packages. However, is there a way to manually do this?

For example, say I define something like this:

```julia
function plot_predictions(model, xs)
    plot(xs, predict(model, xs))
end

```

Then say I am using a few different packages with different models

```julia
using Foo # defines ModelFoo and predict(::ModelFoo, xs)
using Bar # defines ModelBar and predict(::ModelBar, xs)

```

Unfortunately, the packages define different predict functions, meaning that when we try to run the `plot_predictions` function, we end up with an error saying we need to qualify `predict` (`Foo.predict` or `Bar.predict`).

Is there a way I can do something like `predict = merge(Foo.predict, Bar.predict)` where the method table of the new function is created by merging the method tables of the arguments?

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [April 21, 2021, 8:14am UTC](https://discourse.julialang.org/t/manually-merge-functions/59713/2 "2021-04-21T08:14:04Z")

</div>

You could write a new function that checks which of the `predict` functions supports the given argument types and then call that one. Something like the following:

```julia
function my_predict(model, xs)
    if applicable(Foo.predict, model, xs)
        Foo.predict(model, xs)
    elseif applicable(Bar.predict, model, xs)
        Bar.predict(model, xs)
    else
        error("No method found")
    end
end

```

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [April 21, 2021, 4:09pm UTC](https://discourse.julialang.org/t/manually-merge-functions/59713/3 "2021-04-21T16:09:35Z")

</div>

I have written a package which defines a macro `using_merge` which does the merging you want when  
loading packages.

My package is not yet registered but you can use it by

```julia
]add "https://github.com/jmichel7/UsingMerge.jl"

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 21, 2021, 5:03pm UTC](https://discourse.julialang.org/t/manually-merge-functions/59713/4 "2021-04-21T17:03:03Z")

</div>

And I have written a package that defines the same macro but for other types of arguments. If only there was a way to merge them.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [April 21, 2021, 5:05pm UTC](https://discourse.julialang.org/t/manually-merge-functions/59713/5 "2021-04-21T17:05:39Z")

</div>

Can you give a link to your package, please?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 21, 2021, 5:08pm UTC](https://discourse.julialang.org/t/manually-merge-functions/59713/6 "2021-04-21T17:08:29Z")

</div>

Sorry, that was a bit of a joke.
