# Unexpected extension of methods

**URL:** <https://discourse.julialang.org/t/unexpected-extension-of-methods/63312>\
**Category:** New to Julia\
**Tags:** function, functions\
**Created:** [June 21, 2021, 4:25pm UTC](https://discourse.julialang.org/t/unexpected-extension-of-methods/63312 "2021-06-21T16:25:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 21, 2021, 4:25pm UTC](https://discourse.julialang.org/t/unexpected-extension-of-methods/63312/1 "2021-06-21T16:25:05Z")

</div>

Hi you all,

I’ve just encountered an error but I cannot figure out what’s the reason.

I’d like to extend method only when some condition is satisfied. More specifically, if `func` has no certain method, `simfunc` would not be extended.

```julia
function apply_inputs(func; kwargs...)
    simfunc(dx, x, p, t) = func(dx, x, p, t;
                                map(f -> maybe_apply(f, x, p, t), (; kwargs...))...)
    # for log functions
    if hasmethod(func, Tuple{Any, Any, Any, Any, __LOG_INDICATOR__ })
        @show "ih"
        simfunc(dx, x, p, t, __log__ indicator:: __LOG_INDICATOR__ ) = func(dx, x, p, t, __log__ indicator;
                                                                         map(f -> maybe_apply(f, x, p, t), (; kwargs...))...)
    end
    return simfunc
end

```

(`@show "ih"` is for debugging; I checked that “ih” is not printed).

However, after returning `simfunc`, it acts as the method is extended.  
For example, the `simfunc` is passed the following loop as `dyn`:

```julia
            if hasmethod(dyn, Tuple{Any, Any, Any, Any, __LOG_INDICATOR__ })
                @show "hi"
                log_func = function (x, t, integrator::DiffEqBase.DEIntegrator; kwargs...)
                    x = copy(x) # `x` merely denotes a "view"
                    dyn(zero.(x), x, integrator.p, t, __log__ indicator; kwargs...)
                end
                cb_save = SavingCallback(log_func, saved_values;
                                         saveat=saveat, tdir=Int(sign(tspan[2]-tspan[1])))
            end

```

And it prints “hi”, which is unexpected.

- Notes

Here’s [the commit](https://github.com/JinraeKim/FlightSims.jl/commit/eabad6108f69b820fb4e7da27be5bba87d43eecc) and [the test file](https://github.com/JinraeKim/FlightSims.jl/blob/prepare-v0.7/test/lqr.jl) about this issue.  
Please take a look at it.

- Error message

```julia
julia> test()
"hi" = "hi"
ERROR: MethodError: no method matching (::var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}})(::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64}, ::Float64, :: __LOG_INDICATOR__ ; u=[-4.464101615137757])
Closest candidates are:
  (::var"#dynamics!#5")(::Any, ::Any, ::Any, ::Any; u) at /Users/jinrae/.julia/dev/FlightSims/test/lqr.jl:31
Stacktrace:
  [1] (::FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}})(dx::Vector{Float64}, x::Vector{Float64}, p::Vector{Float64}, t::Float64, __log__ indicator:: __LOG_INDICATOR__ )
    @ FlightSims ~/.julia/dev/FlightSims/src/APIs/simulation.jl:75
  [2] (::FlightSims.var"#4#7"{FlightSims.var"#4#5#8"{FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, __LOG_INDICATOR__ }})(x::Vector{Float64}, t::Float64, integrator::OrdinaryDiffEq.ODEIntegrator{CompositeAlgorithm{Tuple{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, true, Vector{Float64}, Nothing, Float64, Vector{Float64}, Float64, Float64, Float64, Float64, Vector{Vector{Float64}}, OrdinaryDiffEq.ODECompositeSolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Vector{Float64}, Vector{Vector{Vector{Float64}}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, CompositeAlgorithm{Tuple{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, OrdinaryDiffEq.CompositeInterpolationData{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Vector{Float64}}, Vector{Float64}, Vector{Vector{Vector{Float64}}}, OrdinaryDiffEq.CompositeCache{Tuple{OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}, OrdinaryDiffEq.Rosenbrock23Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, Matrix{Float64}, Matrix{Float64}, OrdinaryDiffEq.Rosenbrock23Tableau{Float64}, SciMLBase.TimeGradientWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Float64}, Vector{Float64}}, SciMLBase.UJacobianWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Float64, Vector{Float64}}, DefaultLinSolve, FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:forward}(), Float64}, FiniteDiff.GradientCache{Nothing, Vector{Float64}, Vector{Float64}, Float64, Val{:forward}(), Float64, Val{true}()}}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}}, DiffEqBase.DEStats}, ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, OrdinaryDiffEq.CompositeCache{Tuple{OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}, OrdinaryDiffEq.Rosenbrock23Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, Matrix{Float64}, Matrix{Float64}, OrdinaryDiffEq.Rosenbrock23Tableau{Float64}, SciMLBase.TimeGradientWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Float64}, Vector{Float64}}, SciMLBase.UJacobianWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Float64, Vector{Float64}}, DefaultLinSolve, FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:forward}(), Float64}, FiniteDiff.GradientCache{Nothing, Vector{Float64}, Vector{Float64}, Float64, Val{:forward}(), Float64, Val{true}()}}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, OrdinaryDiffEq.DEOptions{Float64, Float64, Float64, Float64, PIController{Rational{Int64}}, typeof(DiffEqBase.ODE_DEFAULT_NORM), typeof(opnorm), Nothing, CallbackSet{Tuple{}, Tuple{DiscreteCallback{DiffEqCallbacks.var"#44#49"{Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#46#51"{Bool, DiffEqCallbacks.var"#48#53"{Bool}, Float64, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, Base.RefValue{Int64}, Base.RefValue{Float64}}, typeof(DiffEqBase.FINALIZE_DEFAULT)}, DiscreteCallback{DiffEqCallbacks.var"#30#31", DiffEqCallbacks.SavingAffect{FlightSims.var"#4#7"{FlightSims.var"#4#5#8"{FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, __LOG_INDICATOR__ }}, Float64, Dict, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.saving_initialize), typeof(DiffEqBase.FINALIZE_DEFAULT)}}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryMinHeap{Float64}, DataStructures.BinaryMinHeap{Float64}, Nothing, Nothing, Int64, Tuple{}, Tuple{}, Tuple{}}, Vector{Float64}, Float64, Nothing, OrdinaryDiffEq.DefaultInit}; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ FlightSims ~/.julia/dev/FlightSims/src/APIs/simulation.jl:33
  [3] (::FlightSims.var"#4#7"{FlightSims.var"#4#5#8"{FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, __LOG_INDICATOR__ }})(x::Vector{Float64}, t::Float64, integrator::OrdinaryDiffEq.ODEIntegrator{CompositeAlgorithm{Tuple{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, true, Vector{Float64}, Nothing, Float64, Vector{Float64}, Float64, Float64, Float64, Float64, Vector{Vector{Float64}}, OrdinaryDiffEq.ODECompositeSolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Vector{Float64}, Vector{Vector{Vector{Float64}}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, CompositeAlgorithm{Tuple{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, OrdinaryDiffEq.CompositeInterpolationData{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Vector{Float64}}, Vector{Float64}, Vector{Vector{Vector{Float64}}}, OrdinaryDiffEq.CompositeCache{Tuple{OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}, OrdinaryDiffEq.Rosenbrock23Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, Matrix{Float64}, Matrix{Float64}, OrdinaryDiffEq.Rosenbrock23Tableau{Float64}, SciMLBase.TimeGradientWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Float64}, Vector{Float64}}, SciMLBase.UJacobianWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Float64, Vector{Float64}}, DefaultLinSolve, FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:forward}(), Float64}, FiniteDiff.GradientCache{Nothing, Vector{Float64}, Vector{Float64}, Float64, Val{:forward}(), Float64, Val{true}()}}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}}, DiffEqBase.DEStats}, ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, OrdinaryDiffEq.CompositeCache{Tuple{OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}, OrdinaryDiffEq.Rosenbrock23Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, Matrix{Float64}, Matrix{Float64}, OrdinaryDiffEq.Rosenbrock23Tableau{Float64}, SciMLBase.TimeGradientWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Float64}, Vector{Float64}}, SciMLBase.UJacobianWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Float64, Vector{Float64}}, DefaultLinSolve, FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:forward}(), Float64}, FiniteDiff.GradientCache{Nothing, Vector{Float64}, Vector{Float64}, Float64, Val{:forward}(), Float64, Val{true}()}}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, OrdinaryDiffEq.DEOptions{Float64, Float64, Float64, Float64, PIController{Rational{Int64}}, typeof(DiffEqBase.ODE_DEFAULT_NORM), typeof(opnorm), Nothing, CallbackSet{Tuple{}, Tuple{DiscreteCallback{DiffEqCallbacks.var"#44#49"{Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#46#51"{Bool, DiffEqCallbacks.var"#48#53"{Bool}, Float64, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, Base.RefValue{Int64}, Base.RefValue{Float64}}, typeof(DiffEqBase.FINALIZE_DEFAULT)}, DiscreteCallback{DiffEqCallbacks.var"#30#31", DiffEqCallbacks.SavingAffect{FlightSims.var"#4#7"{FlightSims.var"#4#5#8"{FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, __LOG_INDICATOR__ }}, Float64, Dict, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.saving_initialize), typeof(DiffEqBase.FINALIZE_DEFAULT)}}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryMinHeap{Float64}, DataStructures.BinaryMinHeap{Float64}, Nothing, Nothing, Int64, Tuple{}, Tuple{}, Tuple{}}, Vector{Float64}, Float64, Nothing, OrdinaryDiffEq.DefaultInit})
    @ FlightSims ~/.julia/dev/FlightSims/src/APIs/simulation.jl:32
  [4] (::DiffEqCallbacks.SavingAffect{FlightSims.var"#4#7"{FlightSims.var"#4#5#8"{FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, __LOG_INDICATOR__ }}, Float64, Dict, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}})(integrator::OrdinaryDiffEq.ODEIntegrator{CompositeAlgorithm{Tuple{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, true, Vector{Float64}, Nothing, Float64, Vector{Float64}, Float64, Float64, Float64, Float64, Vector{Vector{Float64}}, OrdinaryDiffEq.ODECompositeSolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Vector{Float64}, Vector{Vector{Vector{Float64}}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, CompositeAlgorithm{Tuple{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, OrdinaryDiffEq.CompositeInterpolationData{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Vector{Float64}}, Vector{Float64}, Vector{Vector{Vector{Float64}}}, OrdinaryDiffEq.CompositeCache{Tuple{OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}, OrdinaryDiffEq.Rosenbrock23Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, Matrix{Float64}, Matrix{Float64}, OrdinaryDiffEq.Rosenbrock23Tableau{Float64}, SciMLBase.TimeGradientWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Float64}, Vector{Float64}}, SciMLBase.UJacobianWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Float64, Vector{Float64}}, DefaultLinSolve, FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:forward}(), Float64}, FiniteDiff.GradientCache{Nothing, Vector{Float64}, Vector{Float64}, Float64, Val{:forward}(), Float64, Val{true}()}}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}}, DiffEqBase.DEStats}, ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, OrdinaryDiffEq.CompositeCache{Tuple{OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}, OrdinaryDiffEq.Rosenbrock23Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, Matrix{Float64}, Matrix{Float64}, OrdinaryDiffEq.Rosenbrock23Tableau{Float64}, SciMLBase.TimeGradientWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Float64}, Vector{Float64}}, SciMLBase.UJacobianWrapper{ODEFunction{true, FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Float64, Vector{Float64}}, DefaultLinSolve, FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:forward}(), Float64}, FiniteDiff.GradientCache{Nothing, Vector{Float64}, Vector{Float64}, Float64, Val{:forward}(), Float64, Val{true}()}}}, OrdinaryDiffEq.AutoSwitchCache{Tsit5, Rosenbrock23{0, false, DefaultLinSolve, DataType}, Rational{Int64}, Int64}}, OrdinaryDiffEq.DEOptions{Float64, Float64, Float64, Float64, PIController{Rational{Int64}}, typeof(DiffEqBase.ODE_DEFAULT_NORM), typeof(opnorm), Nothing, CallbackSet{Tuple{}, Tuple{DiscreteCallback{DiffEqCallbacks.var"#44#49"{Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#46#51"{Bool, DiffEqCallbacks.var"#48#53"{Bool}, Float64, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, Base.RefValue{Int64}, Base.RefValue{Float64}}, typeof(DiffEqBase.FINALIZE_DEFAULT)}, DiscreteCallback{DiffEqCallbacks.var"#30#31", DiffEqCallbacks.SavingAffect{FlightSims.var"#4#7"{FlightSims.var"#4#5#8"{FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, __LOG_INDICATOR__ }}, Float64, Dict, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.saving_initialize), typeof(DiffEqBase.FINALIZE_DEFAULT)}}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryMinHeap{Float64}, DataStructures.BinaryMinHeap{Float64}, Nothing, Nothing, Int64, Tuple{}, Tuple{}, Tuple{}}, Vector{Float64}, Float64, Nothing, OrdinaryDiffEq.DefaultInit}, force_save::Bool)
    @ DiffEqCallbacks ~/.julia/packages/DiffEqCallbacks/VjyxU/src/saving.jl:63
  [5] SavingAffect
    @ ~/.julia/packages/DiffEqCallbacks/VjyxU/src/saving.jl:46 [inlined]
  [6] apply_discrete_callback!
    @ ~/.julia/packages/DiffEqBase/kGakm/src/callbacks.jl:830 [inlined]
  [7] apply_discrete_callback!
    @ ~/.julia/packages/DiffEqBase/kGakm/src/callbacks.jl:851 [inlined]

...

 [15] #solve_call#58
    @ ~/.julia/packages/DiffEqBase/kGakm/src/solve.jl:61 [inlined]
 [16] #solve_up#60
    @ ~/.julia/packages/DiffEqBase/kGakm/src/solve.jl:88 [inlined]
 [17] #solve#59
    @ ~/.julia/packages/DiffEqBase/kGakm/src/solve.jl:70 [inlined]
 [18] sim(state0::Vector{Float64}, dyn::FlightSims.var"#simfunc#15"{Base.Iterators.Pairs{Symbol, FlightSims.var"#103#104"{Matrix{Float64}}, Tuple{Symbol}, NamedTuple{(:u,), Tuple{FlightSims.var"#103#104"{Matrix{Float64}}}}}, var"#dynamics!#5"{var"#dynamics!#3#6"{LinearSystemEnv}}}, p::Vector{Float64}; t0::Float64, tf::Float64, solver::Nothing, callback::DiscreteCallback{DiffEqCallbacks.var"#44#49"{Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, DiffEqCallbacks.var"#46#51"{Bool, DiffEqCallbacks.var"#48#53"{Bool}, Float64, DiffEqCallbacks.var"#45#50"{var"#affect!#4", Float64, Base.RefValue{Int64}, Base.RefValue{Float64}}, Base.RefValue{Int64}, Base.RefValue{Float64}}, typeof(DiffEqBase.FINALIZE_DEFAULT)}, log_off::Bool, saveat::Nothing, savestep::Float64, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ FlightSims ~/.julia/dev/FlightSims/src/APIs/simulation.jl:43
 [19] test()
    @ Main ~/.julia/dev/FlightSims/test/lqr.jl:36
 [20] top-level scope
    @ REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 21, 2021, 6:50pm UTC](https://discourse.julialang.org/t/unexpected-extension-of-methods/63312/2 "2021-06-21T18:50:31Z")

</div>

This is yet another case of [Confused by behaviour of nested functions](https://discourse.julialang.org/t/confused-by-behaviour-of-nested-functions/20576) , which is definitely an unfortunate gotcha of the language. As discussed in that thread, it’s usually better to conditionally return a different anonymous function rather than conditionally adding or redefining methods like that.

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 21, 2021, 7:01pm UTC](https://discourse.julialang.org/t/unexpected-extension-of-methods/63312/3 "2021-06-21T19:01:15Z")

</div>

What if I want to pass methods (at once as an argument)?  
Is it impossible?
