# MethodError: no method matching, but the method exists

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 7, 2021, 12:27am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767 "2021-02-07T00:27:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CFS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfs/32/14601_2.png) [@CFS](https://discourse.julialang.org/u/CFS)\
**Post date:** [February 7, 2021, 12:27am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/1 "2021-02-07T00:27:09Z")

</div>

I’m getting the following error:

```julia
ERROR: MethodError: no method matching choose(::Main.Sim.ModelBasedAgent, ::Int64)
Closest candidates are:
  choose(::Main.Sim.ModelFreeAgent, ::Int64) at /home/xxxxx/Projects/simulateagents.jl:57
  choose(::Main.Sim.ModelBasedAgent, ::Int64) at /home/xxxxx/Projects/simulateagents.jl:53

```

But there is a matching method. What’s going on?

---

<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:** [February 7, 2021, 2:13am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/2 "2021-02-07T02:13:56Z")

</div>

First of all, welcome to our community! 🎊

This kind of error often happens if you are using `include` to include the same file in many parts of your code (`include` should not be used this way, if this is the case I can point some references to you later). You will end up creating the same **type** multiple times and they all have the same name and fields but will be incompatible to each other. Without more information is hard to draw a conclusion. Could you provide/point out to your sources (or give an MWE)?

---

<div class="post-metadata">

**Author:** ![CFS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfs/32/14601_2.png) [@CFS](https://discourse.julialang.org/u/CFS)\
**Post date:** [February 7, 2021, 10:00am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/3 "2021-02-07T10:00:40Z")

</div>

You are right, I had included the file several times in the REPL. Thank you! But then how do you reinclude a file after changing it?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 7, 2021, 11:17am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/4 "2021-02-07T11:17:27Z")

</div>

You just need to make sure to call `include` at the toplevel, not inside the same function you are calling the included function from.

---

<div class="post-metadata">

**Author:** ![CFS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfs/32/14601_2.png) [@CFS](https://discourse.julialang.org/u/CFS)\
**Post date:** [February 7, 2021, 11:49am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/5 "2021-02-07T11:49:07Z")

</div>

I’m not including anything in the file containing my code, I’m including this file in the REPL multiple times (after changing the code).

---

<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:** [February 7, 2021, 12:27pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/6 "2021-02-07T12:27:53Z")

</div>

One option is to use [`includet`](https://timholy.github.io/Revise.jl/stable/user_reference/#Revise.includet) from package [`Revise.jl`](https://github.com/timholy/Revise.jl). You just do the `includet` a single time, and then when you change and save the file, `Revise.jl` will silently update the code in the REPL for you.

However, `includet` only tracks changes to methods, so this will work if you included the same file multiple times because you were changing function code, and ended with the same type multiple times because the definition was there too, but **this will not work if you were making changes to the type definition too**.

If you change a type definition (`struct`/`mutable struct`) then you have to restart the REPL, otherwise the Julia session may be lost at which version of the type you are referring to. (Objects created with an old version of the type will have the same name, but dispatch on the functions defined together with the new type will fail on them, like you perceived.) There are some tricks to deal with changing a type in the middle of a REPL session, but they are considerably limited:

1. Do not use your type. Use a `NamedTuple` instead. Dispatch your functions over `NamedTuple`, and change your fields as much as you want. When you are sure the fields will not change anymore, then replace `NamedTuple` by your type.
2. Create an abstract type, dispatch your functions on it, if you make a change to the struct instead create a new `struct` ending with `_2`, `_3`, and so on, all these inherit the abstract type so your functions recognize them,
3. Put the `struct` and functions inside a `module`. When you need to update the `struct`, change the `module` name too, `include` again, and prefix with the new `module` name.

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [April 12, 2021, 12:12am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/7 "2021-04-12T00:12:21Z")

</div>

I had this problem when using JupyterLab (version 2.2.6), but not when running the same code directly in Juila.

I only had includes at the top-level, and apparently wasn’t doing anything else wrong. Either JupyterLab sometimes preforms commands in an unexpected context, it got wedged when an earlier include failed, or it’s a heisenbug.

I spent a lot of time thinking that I didn’t understand dispatch and trying random variations before realizing that the problem only occurred in JupyterLab.

---

<div class="post-metadata">

**Author:** ![CFS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfs/32/14601_2.png) [@CFS](https://discourse.julialang.org/u/CFS)\
**Post date:** [April 12, 2021, 1:41pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/8 "2021-04-12T13:41:54Z")

</div>

I wasn’t using Jupyter though.

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [April 12, 2021, 7:30pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767/9 "2021-04-12T19:30:30Z")

</div>

Understood.

I assume that your problem and my problem are related. Although I don’t know precisely what causes either.

The comments suggest that there is a bug, that is only exercised when you do something unusual, probably involving issuing the same include multiple times. But I suspect that whatever surfaces this bug, Jupyter does it somewhat often. As a result this may be a more common bug than is widely understood.
