# Multiple dispatch with module and external functions

**URL:** https://discourse.julialang.org/t/multiple-dispatch-with-module-and-external-functions/53694
**Category:** New to Julia
**Tags:** function
**Created:** [January 21, 2021, 12:18am UTC](https://discourse.julialang.org/t/multiple-dispatch-with-module-and-external-functions/53694 "2021-01-21T00:18:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Abhishek\_Goudar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhishek_goudar/32/21296_2.png) [@Abhishek\_Goudar](https://discourse.julialang.org/u/Abhishek_Goudar)
#### Post date: [January 21, 2021, 12:18am UTC](https://discourse.julialang.org/t/multiple-dispatch-with-module-and-external-functions/53694/1 "2021-01-21T00:18:51Z")

</div>

Hello everyone,

Julia newbie here. My problem revolves around multiple dispatch. The following is a pseudo-code and might not adhere to the proper syntax.

custom\_module.jl

```julia
module custom_module

export Residual
export geterror!
export solve

abstract type Residual end

function geterror!(res::T) where {T<: Residual}
  println("In base function method")
end

function solve(res::T) where {T <: Residual}
     geterror!(res)
end

end

```

test.jl

```julia
using custom_module

mutable struct MutatedResidual <: Residual
end

function geterror!(res::MutatedResidual)
  println("In mutated residual")
end

solve(MutatedResidual())

```

The expectation is that the function with `MutatedResidual ` argument is called. Instead the base method within the module is called. One option to solve this is to do `geterrror!(res::T) where {T<:Residual} = geterrror!(res::MutatedResidual)`. However, if I have multiple mutations of `Residual` with each having its own overloaded version of `geterror!` then each assignment `geterrror!(res::T) where {T<:Residual} = geterrror!(res::MutatedResidual)` will override the previous one. Basically, I want to be able to do multiple dispatch by exposing the functions outside the module to the module so that it calls the appropriate functions (as determined by the type of the argument).

If the details are unclear, I am happy to provide more details. Also, if I am violating community guidelines with the format/nature/content of the question, please do critique.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 21, 2021, 12:28am UTC](https://discourse.julialang.org/t/multiple-dispatch-with-module-and-external-functions/53694/2 "2021-01-21T00:28:59Z")

</div>

I think that to add methods to the function of the module you need to explicitly import it:

```julia
import custom_module.geterror
geterror(res::MutatedResidual) = ...

```

---

<div class="post-metadata">

### Author: ![Abhishek\_Goudar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhishek_goudar/32/21296_2.png) [@Abhishek\_Goudar](https://discourse.julialang.org/u/Abhishek_Goudar)
#### Post date: [January 21, 2021, 12:39am UTC](https://discourse.julialang.org/t/multiple-dispatch-with-module-and-external-functions/53694/3 "2021-01-21T00:39:47Z")

</div>

That is correct. But I have multiple definitions of `geterror!` for different arguments and assigning a _version_ of `geterror!` will override any previous assignments. However, I want `custom_module` to _see_ all the variants of `geterror!` not just the one assigned, so that `solve` can call appropriate versions.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 21, 2021, 12:48am UTC](https://discourse.julialang.org/t/multiple-dispatch-with-module-and-external-functions/53694/4 "2021-01-21T00:48:11Z")

</div>

I think that will work as you expect, if you add a method to the function for the new types you defined. I mean exactly as in your example but importing the function before defining the method. I think that works. Sorry not being able to test it now.

An alternative is to add the method explicitly to the module function:

```julia
custom_module.geterror(res::MutatedResidual) = ...

```

That should work as well.

---

<div class="post-metadata">

### Author: ![Abhishek\_Goudar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhishek_goudar/32/21296_2.png) [@Abhishek\_Goudar](https://discourse.julialang.org/u/Abhishek_Goudar)
#### Post date: [January 21, 2021, 12:59am UTC](https://discourse.julialang.org/t/multiple-dispatch-with-module-and-external-functions/53694/5 "2021-01-21T00:59:40Z")

</div>

Apologies. I had a typo. `import cumstom_module.geterror!` does the job. Thank you @lmiq. I need to faimiliarize myself with the intricacies of `using` and `import`.
