# Methods imported from submodules are overwritten, not extended

**URL:** <https://discourse.julialang.org/t/methods-imported-from-submodules-are-overwritten-not-extended/87995>\
**Category:** General Usage\
**Tags:** module, dispatch, multidispatch, namespaces\
**Created:** [September 29, 2022, 4:02pm UTC](https://discourse.julialang.org/t/methods-imported-from-submodules-are-overwritten-not-extended/87995 "2022-09-29T16:02:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)\
**Post date:** [September 29, 2022, 4:02pm UTC](https://discourse.julialang.org/t/methods-imported-from-submodules-are-overwritten-not-extended/87995/1 "2022-09-29T16:02:10Z")

</div>

I assumed that by defining different argument requirements I would get different methods for an already imported function from a submodule, but it seems I am getting an overwrite instead of an extension. What’s the idiomatic way of achieving this? I know about the @reexport macro, but I only want to achieve this selectively and I prefer to minimize dependencies.

Here’s a MWE:

```julia
module ParentModule

export test_func

struct Astruct
end

module SubModule

export test_func

struct Bstruct
end

test_func(b::Bstruct) = println("method B")

end

using .SubModule

 test_func(a::Astruct) = println("method A")

end

```

When we write test\_func on the REPL to see the available methods:

```julia
julia> using .ParentModule

julia> test_func(
test_func(a::Main.ParentModule.Astruct) in Main.ParentModule at REPL[1]:24

```

If we repeat the same exercise, having commented the definition inside ParentModule, we get the method defined inside the submodule:

```julia
julia> module ParentModule

       export test_func

       struct Astruct
       end

       module SubModule

       export test_func

       struct Bstruct
       end

       test_func(b::Bstruct) = println("method B")

       end

       using .SubModule

       # test_func(a::Astruct) = println("method A")

       end
Main.ParentModule

julia> using .ParentModule

julia> test_func(
test_func(b::Main.ParentModule.SubModule.Bstruct) in Main.ParentModule.SubModule at REPL[1]:18

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 29, 2022, 4:40pm UTC](https://discourse.julialang.org/t/methods-imported-from-submodules-are-overwritten-not-extended/87995/2 "2022-09-29T16:40:51Z")

</div>

You either have to

```julia
      import .SubModule2: test_func2

      test_func2(a::Astruct) = println("method A")

```

or explicitly specify that you’re overriding `SubModule`’s function:

```julia
       using .SubModule

       SubModule.test_func(a::Astruct) = println("method A")

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 29, 2022, 4:43pm UTC](https://discourse.julialang.org/t/methods-imported-from-submodules-are-overwritten-not-extended/87995/3 "2022-09-29T16:43:42Z")

</div>

See also:

> <https://stackoverflow.com/questions/27086159/what-is-the-difference-between-using-and-import-in-julia-when-building-a-mod/61072552#61072552>

---

<div class="post-metadata">

**Author:** ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)\
**Post date:** [September 30, 2022, 2:00pm UTC](https://discourse.julialang.org/t/methods-imported-from-submodules-are-overwritten-not-extended/87995/4 "2022-09-30T14:00:06Z")

</div>

Thank you, I had not realized the extensible vs in-scope differences.
