# Step!() remains strangely unfound if buried inside another method

**URL:** <https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316>\
**Category:** General Usage\
**Tags:** question, scope\
**Created:** [January 3, 2024, 8:56pm UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316 "2024-01-03T20:56:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [January 3, 2024, 8:56pm UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316/1 "2024-01-03T20:56:13Z")

</div>

Hi. Can anyone please tell me why the call to `step!(irg.odes)` in the following code generates the error:  
`MethodError: no method matching step!(::DynamicalSystemsBase.CoupledODEs{true, ...`?

Note that the call to `reinit!(irg.odes)` does _not_ raise an error! 🙃

```julia
module Test
using DynamicalSystems

mutable struct IRGame
	odes::CoupledODEs
	function IRGame()
		new( CoupledODEs(
			function (du,u,p,t)
				du[1] = p[:alpha] * u[1]
				nothing
			end,
			[1.0],
			Dict(:alpha=>0.1)
		))
	end
end

function step!( irg::IRGame)
	reinit!( irg.odes)
	step!( irg.odes)
end

function demo()
	game = IRGame()
	step!(game)
end

end

```

---

<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 3, 2024, 9:02pm UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316/2 "2024-01-03T21:02:08Z")

</div>

> [@Niall](#):
>
> `function step!( irg::IRGame)`

You need to define this as `function DynamicalSystems.step!(irg::IRGame)` if you intend to add a new method to `DynamicalSystems.step!`, as otherwise you are declaring a completely unrelated `step!` function in your module that shadows the former function.

(Alternatively, call `DynamicalSystems.step!(irg.odes)` to explicitly call the `step!` function from the `DynamicalSystems` module.)

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [January 4, 2024, 9:25am UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316/3 "2024-01-04T09:25:59Z")

</div>

Many thanks, @stevengi - that works beautifully. However, there is apparently some newby issue that I’m still not understanding: what is it that prevents multiple dispatch from distinguishing between my `step!` and `DynamicalSystems.step!` on the basis of their different argument types (`IRGame` and `CoupledODEs`)?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 4, 2024, 9:47am UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316/4 "2024-01-04T09:47:01Z")

</div>

As Steven said you are shadowing the function - think of it as overwriting the definition, so `step!` is not related to `DynamicalSystems.step!` at all.

Here’s a simple example:

```julia
julia> struct A end

julia> sum(x::A) = 5
sum (generic function with 1 method)

julia> sum(A())
5

julia> sum([1,2,3])
ERROR: MethodError: no method matching sum(::Vector{Int64})
You may have intended to import Base.sum

```

As you see, the `sum` I created there has only 1 method, so there is nothing for multiple dispatch to do. For multiple dispatch to work I’d have import `Base.sum` and extend it, like here (new session):

```julia
julia> struct A end

julia> import Base.sum

julia> Base.sum(x::A) = 5

julia> sum(A())
5

julia> sum([1,2,3])
6

```

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [January 4, 2024, 10:27am UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316/5 "2024-01-04T10:27:47Z")

</div>

OK, I see. So the dispatcher only gets to do its work if the relevant functions are members of the same (or having common descent?) modules, is that right?

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [January 8, 2024, 9:16am UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316/6 "2024-01-08T09:16:42Z")

</div>

Yes.
