Unexpected extension of methods

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.

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:

            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 and the test file about this issue.
Please take a look at it.

  • Error message
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

This is yet another case of Confused by behaviour of nested functions , 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.

3 Likes

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