# Why doesn't method dispatch work on the same candidate included from different sources?

**URL:** <https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 10, 2022, 8:35am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576 "2022-08-10T08:35:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 8:35am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/1 "2022-08-10T08:35:11Z")

</div>

I originally made the [question on SO](https://stackoverflow.com/questions/73295624/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-s) but nobody could help.

I have this error:

```julia

ERROR: MethodError: no method matching 
correct_field(::Main.Poetry.LoadTemplates.TicketTemplates.myStruct, ::Dict{String, Any})
Closest candidates are:
  correct_field(::Main.Poetry.ContentGen.TicketTemplates.myStruct, ::Any)
  (...)

```

The problem is that I loaded the `structs` in `LoadTemplates` and now I am using a generic function from `ContentGen` that dispatches on those structs.

How is this usually solved? I guess I can “unmodulate” my Julia files. Or maybe there’s a way to chop off all those `Main.Poetry...` to get just `TicketTemplates.myStruct`.

Finally, I’d like to know what’s behind this design decision and what are the use cases.

---

<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:** [August 10, 2022, 8:41am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/2 "2022-08-10T08:41:36Z")

</div>

If you check the types of your objects, you’ll notice that they have different module hierarchies:

```julia
::Main.Poetry.LoadTemplates.TicketTemplates.myStruct
::Main.Poetry.LoadTemplates.ContentGen.myStruct

```

So the two structs are _not_ the same. I’m guessing you `include`ed a file defining that struct twice, once in `TicketTemplates` and once in `ContentGen`? If so, these two definitions define distinct types, due to being in a different namespace due to being in a different module.

---

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 8:48am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/3 "2022-08-10T08:48:01Z")

</div>

Yes, that’s what I say in my question.

What should I do about it?

---

<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:** [August 10, 2022, 8:49am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/4 "2022-08-10T08:49:06Z")

</div>

Don’t just `include` the struct definition twice and instead import the binding via `using` or `import` from the module.

[https://docs.julialang.org/en/v1/manual/modules/#Submodules-and-relative-paths](https://docs.julialang.org/en/v1/manual/modules/#Submodules-and-relative-paths)

---

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 8:51am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/5 "2022-08-10T08:51:11Z")

</div>

Damn I spent so much time figuring out how to properly import local modules. I wound up with `include` followed by a `using .MyModule`.

But I think I get it now!  
I have to include before the module begins and then do using inside it.

---

<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:** [August 10, 2022, 8:53am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/6 "2022-08-10T08:53:31Z")

</div>

Yes. `using`/`import` are for navigating the module hierarchy. `include` is for establishing that hierarchy (it’s literally pasting the code from the included file where you wrote `include`). Thus, multiple `include`s literally create new nodes in the module hierarchy - they are not linked to the original file.

---

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 10:38am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/7 "2022-08-10T10:38:02Z")

</div>

This feels unfair @Sukera☹

```julia
ERROR: MethodError: no method matching correct_field(::Main.TicketTemplates.ticketSN, ::Dict{String, Any})
Closest candidates are:
  correct_field(::Main.TicketTemplates.ticketSN, ::Any)

```

What am I missing now?

---

<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:** [August 10, 2022, 10:39am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/8 "2022-08-10T10:39:31Z")

</div>

That’s odd 🤔 How are you defining those methods & that struct? Do you have a MWE?

---

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 10:51am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/9 "2022-08-10T10:51:34Z")

</div>

I’m not sure what’s a `MWE` I tried googling it but to no avail, that should answer your question 😃

### A minimal reproducible example follows.

File `structs.jl`:

```julia
module Structs
export myStruct

struct myStruct
end

end

```

File `load.jl`:

```julia
include("structs.jl")
module Load
using Main.Structs
export load_struct

function load_struct()
    myStruct()
end

end

```

File `methods.jl`:

```julia-auto
include("structs.jl")

module Methods
using Main.Structs
export method
function method(strt::myStruct)
    "I got called"
end
end

```

File `poet.jl`:

```julia-auto
include("methods.jl")
include("load.jl")
module Poet
using Main.Methods
using Main.Load

strt = load_struct()
method(strt)
end

```

Running `poet.jl` yields:

```julia-auto
ERROR: MethodError: no method matching method(::Main.Structs.myStruct)
Closest candidates are:
  method(::Main.Structs.myStruct) 

```

---

<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:** [August 10, 2022, 11:00am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/10 "2022-08-10T11:00:25Z")

</div>

> [@DiMeneses](#):
>
> I’m not sure what’s a `MWE` I tried googling it but to no avail, that should answer your question 😃

It’s a [(M)inimal (W)orking (E)xample](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

> [@DiMeneses](#):
>
> File `methods.jl`:
> 
> ```julia
> include("structs.jl")
> 
> ```

> [@DiMeneses](#):
>
> File `load.jl`:
> 
> ```julia
> include("structs.jl")
> 
> ```

You’re still including that file twice, which will still create two modules. The first module will be the one `Load` sees and binds to, then in `methods.jl` you create a _new_ `Structs` module which is what `Methods` sees and binds to (and which overwrites the binding created in `load.jl`).

I don’t know why you’re creating submodules for the structs in particular, I’d just have them defined like so:

> **poet.jl**
>
> ```julia
> module Poet
> include("structs.jl")
> include("methods.jl")
> end
> 
> ```

> **structs.jl**
>
> ```julia
> struct myStruct
> ...
> end
> 
> ```

> **modules.jl**
>
> ```julia
> function method(strt::myStruct)
> "I got called"
> end
> 
> ```

---

<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:** [August 10, 2022, 11:07am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/11 "2022-08-10T11:07:39Z")

</div>

Or, if you insist on the module hierarchy:

> **poet.jl**
>
> ```julia
> module Poet
> include("structs.jl")
> include("methods.jl")
> end
> 
> ```

> **structs.jl**
>
> ```julia
> module Structs
> 
> export myStruct
> 
> struct myStruct
> ...
> end
> 
> end
> 
> ```

> **modules.jl**
>
> ```julia
> module Methods
> 
> using ..Structs
> 
> function method(strt::myStruct)
> "I got called"
> end
> 
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 11:09am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/12 "2022-08-10T11:09:20Z")

</div>

Ah that was funny, a MWE is a MRE.

I thought modules would have equivalent functionality to C’s header guards/`#pragma once`. Hmm.

Putting all includes on `poet.jl` solved the issue. But I find it weird, given that this makes the modules dependent on the client which uses them…

---

<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:** [August 10, 2022, 11:10am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/13 "2022-08-10T11:10:55Z")

</div>

> [@DiMeneses](#):
>
> I thought modules would have equivalent functionality to C’s header guards/`#pragma once`. Hmm.

They are not - they are distinct objects that you can manipulate.

> [@DiMeneses](#):
>
> Putting all includes on `poet.jl` solved the issue. But I find it weird, given that this makes the modules dependent on the client which uses them…

Sounds like there’s more to your motivation than just the `include` stuff? Do you have more context, maybe we’re encountering an [XY-Problem](https://en.wikipedia.org/wiki/XY_problem)?

---

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 11:15am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/14 "2022-08-10T11:15:56Z")

</div>

Thank you for introducing me to the XY-problem name. I knew it but didn’t know it was coined as such

That’s a very real possibility. I am writing a fuzz tester for a C program. I looked around for design patterns but didn’t find any so my current implementation feels sketchy to me.

The fuzz tester simply generates `txt` files following templates and I vary the fields depending if they are evaluated in branching conditions or not, so that I can better cover the code flow.

Eventually I might drop the `structs.jl` file for `json`. But then I’d have to find a better way to match the methods of generic functions.

Maybe I could simply do away with all modules, but that would lead me to my initial annoyance of `struct redefinition error`

---

<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:** [August 10, 2022, 11:25am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/15 "2022-08-10T11:25:37Z")

</div>

I’m not sure I follow. Do you have a small pseudocode example of what you’re trying to do?

> [@DiMeneses](#):
>
> Maybe I could simply do away with all modules, but that would lead me to my initial annoyance of `struct redefinition error`

In what context do you get that error?

---

<div class="post-metadata">

**Author:** ![DiMeneses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dimeneses/32/38691_2.png) [@DiMeneses](https://discourse.julialang.org/u/DiMeneses)\
**Post date:** [August 10, 2022, 11:28am UTC](https://discourse.julialang.org/t/why-doesnt-method-dispatch-work-on-the-same-candidate-included-from-different-sources/85576/16 "2022-08-10T11:28:16Z")

</div>

I’m developing my code in `vscode` shell so whenever I rerun code that redefines the structs.

I just follow the advice on this post: [workspace() is gone](https://discourse.julialang.org/t/how-to-clear-variables-and-or-whole-work-space/10149)

I still have some reading to do on Julia workflow.
