# Two modules with the same exported function name but different signature

**URL:** <https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 20, 2018, 12:40pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231 "2018-09-20T12:40:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 20, 2018, 12:40pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/1 "2018-09-20T12:40:39Z")

</div>

Hello, I have the following situation which I did not expected: defining a fucntion with the same name but different signature in two modules like this:

```julia
## Module RawTables
function save(T::RawTable, name_pref::String)
...
end

## Module RawPlots
function save(T::RawPlot, name_pref::String)
...
end

```

and using them together results in

```julia
WARNING: both RawPlots and RawTables export "save"; uses of it in module Main must be qualified

```

I thought multiple dispatch is exactly good in being able to resolve this issue for me? Basically it is only function overloading in this case and there simply **cannot** be any problems about what method to call.

What is happening here and how can I resolve this? Thanks

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 20, 2018, 12:45pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/2 "2018-09-20T12:45:09Z")

</div>

Just because two generic function (which is the container of the methods) have the same name, does not mean that they are the same object (you can check this with `===`). You need to extend the generic function defined in one module in the other; just like with Base functions:

```julia
julia> module A
       export f
       f() = 1
       end                                                                                                                                                             
Main.A                                                                                                                                                                 

julia> module B
       using ..A
       A.f(x) = 2
       end                                                                                                                                                             
Main.B      
                                                                                                                                                           
julia> using .A                                                                                                                                                        
                                                                                                                                                                       
julia> f()                                                                                                                                                             
1                                                                                                                                                                      
                                                                                                                                                                       
julia> f(1)                                                                                                                                                            
2                                                                                                                                                                      

```

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 20, 2018, 12:51pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/3 "2018-09-20T12:51:39Z")

</div>

Thanks. But this implies that the modules have to know about each other, which is not useful for anything else. And I have to specify the hierarchy which module extends the other one?

Maybe there can be some check whether the name exists and extend it or otherwise create a new object?

I mean, e. g. in this example `save` is a very common name but yet it is not in Base. Does this mean, that everyone needs to know about evry other module which possibly also uses `save` and extend it?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 20, 2018, 1:07pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/4 "2018-09-20T13:07:21Z")

</div>

This has been discussed at length, the latest was [Function name conflict: ADL / function merging?](https://discourse.julialang.org/t/function-name-conflict-adl-function-merging/10335).

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 20, 2018, 1:40pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/5 "2018-09-20T13:40:23Z")

</div>

No; just that you have to know about another module if you want to extend methods of that specific module when developing a module. Otherwise you just have to specify which modules version you intended to use when actually using both modules at the same time.

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 20, 2018, 1:47pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/6 "2018-09-20T13:47:34Z")

</div>

Thank you for the link. Well, I have looked through it and I clearly would vote for a possibility to have common function names from different modules to work together smoothly without the need to import them or state the module explicitly 😉

But this is only my option…

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 20, 2018, 1:50pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/7 "2018-09-20T13:50:46Z")

</div>

> [@stakaz](#):
>
> without the need to import them

They already do; if you only import one of them, that warning is not going to show up. It shows up in the example provided by @mauro3 because they’re both contained in the top level scope `Main` and are referencing each other and are thus loaded by default in the REPL.

> [@stakaz](#):
>
> state the module explicitly

You only have to do this when developing a module and explicitly want to add to that modules functions method tables. You are not required to qualify every module that might potentially have a `save` function.

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 20, 2018, 1:52pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/8 "2018-09-20T13:52:27Z")

</div>

This is exactly the problem I have. Neither do I extend the `save` behavior nor do I want to specify the module name because I call the `save` function with a type which clearly can decide the right version of the function for me.

In my option this is a very clear case of “do the same thing” bit still belong to different modules.

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 20, 2018, 1:56pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/9 "2018-09-20T13:56:28Z")

</div>

I can understand that this can lead to errors on the big scale but for some very common words which are not in `Base` I really would like to see a way to do this. For convenience, not because it is logically the best option for a programming language and all this discussion about “doing the same thing”.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 20, 2018, 2:04pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/10 "2018-09-20T14:04:45Z")

</div>

You should hook into [https://github.com/JuliaIO/FileIO.jl](https://github.com/JuliaIO/FileIO.jl) for load/save.

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 20, 2018, 2:29pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/11 "2018-09-20T14:29:23Z")

</div>

You made my day. A logical and obvious solution which I did not considered. Thanks

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 20, 2018, 2:31pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/12 "2018-09-20T14:31:27Z")

</div>

Cool!

That is one of the pros of not automatically merging stuff: it fosters frameworks which are used by many and thus make the ecosystem more consistent.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 20, 2018, 3:33pm UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/13 "2018-09-20T15:33:10Z")

</div>

> [@stakaz](#):
>
> . Neither do I extend the `save` behavior nor do I want to specify the module name because I call the `save` function with a type which clearly can decide the right version of the function for me.

Dispatching to a specific method of the save function based on the input types is exactly what extending the function is. And knowing what `save` means is necessary in order to write generic code. If everyone has their own version of `push!` with arbitrary meaning, that means that whenever you see some code like

```julia
function g(x)
    for i in 1:100 
        push!(g, i)
    end
end

```

you have no idea what the purpose of it is.

Now you can just:

```julia
help?> push!
search: push! pushfirst! pushdisplay

  push!(collection, items...) -> collection

  Insert one or more items at the end of collection.

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 21, 2018, 5:08am UTC](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231/14 "2018-09-21T05:08:24Z")

</div>

> [@stakaz](#):
>
> Neither do I extend the `save` behavior nor do I want to specify the module name because I call the `save` function with a type which clearly can decide the right version of the function for me.

If you read through that thread, you will notice some workarounds along the lines of

```julia
import A # has f
import B # has f

f(x::SomeType) = A.f(x) # specifically for SomeType
f(x) = B.f(x) # fallback

```

and variations on this theme.

In general: Julia, like most contemporary generic programming languages, is very careful about namespace management. This may seem inconvenient in some cases, but I suppose that after writing a few 10⁴ LOC you will see its value.
