# What is the best way to override a function in different modules?

**URL:** https://discourse.julialang.org/t/what-is-the-best-way-to-override-a-function-in-different-modules/54111
**Category:** New to Julia
**Created:** [January 28, 2021, 11:07am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-override-a-function-in-different-modules/54111 "2021-01-28T11:07:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)
#### Post date: [January 28, 2021, 11:07am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-override-a-function-in-different-modules/54111/1 "2021-01-28T11:07:29Z")

</div>

Hi,

What is the best practice to make sure to avoid the following type of warnings?

```julia
WARNING: both ABM_Sim and Production export "produce!"; uses of it in module Main must be qualified

```

I have 2 modules where I defined the produce!() function, both with different parameters. I use the first module in the second one.

What is best practice to avoid name conflicts and to make maximum use of the multiple dispatch functionality of Julia?

Thanks in advance,  
Stef

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [January 28, 2021, 11:28am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-override-a-function-in-different-modules/54111/2 "2021-01-28T11:28:10Z")

</div>

Unless you import the function name `produce!` from the other module, the two `produce!` are two different functions that just happen to have the same name. If instead you import the function, you’re adding a new method, with a different signature, to the same function

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 28, 2021, 1:00pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-override-a-function-in-different-modules/54111/3 "2021-01-28T13:00:45Z")

</div>

More explicitly, if `ABM_Sim` defines a function `produce!` and you want to add a new method in the module `Production`, you have two options. Either explicitly `import` it:

```julia
import ABC_Sim: produce! # explicitly import produce! to add new methods
function produce!(....) # new method
    ...
end

```

or explicitly qualify the name when you define the new method:

```julia
function ABC_Sim.produce!(....) # new method for ABC_Sim's function
    ...
end

```

Mostly I would suggest use the latter style.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 28, 2021, 1:04pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-override-a-function-in-different-modules/54111/4 "2021-01-28T13:04:04Z")

</div>

PS. Where is this clearly explained in the manual? It doesn’t seem to be mentioned in the [Functions chapter](https://docs.julialang.org/en/v1/manual/functions/) or the [Methods chapter](https://docs.julialang.org/en/v1/manual/methods/), and the [Modules chapter](https://docs.julialang.org/en/v1/manual/modules/) only devotes two sentences to the subject.

_Update_: ah, in the latest version, @Tamas_Papp added some documentation in [https://github.com/JuliaLang/julia/pull/38271](https://github.com/JuliaLang/julia/pull/38271)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [January 28, 2021, 3:39pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-override-a-function-in-different-modules/54111/5 "2021-01-28T15:39:11Z")

</div>

I do this in my package [GuillotineModels.jl](https://github.com/henriquebecker91/GuillotineModels.jl): I have the package module defining some function like `solution_value`, that should have methods implemented for different combinations of problems and instances, and in the inner modules (of the same package, inside the `GuillotineModels` module) I import the `solution_value` and extend it. I do so because I want `solution_value` to be available by people `using` my main module, but at the same time, its methods are better implemented in inner modules that implement the instances for which the methods are specialized, and already have them in scope.
