diff --git a/src/remake.jl b/src/remake.jl index aadb2efd7..a040c61d4 100644 --- a/src/remake.jl +++ b/src/remake.jl @@ -30,14 +30,34 @@ end function _remake_internal(thing; kwargs...) T = remaker_of(thing) + named_thing = struct_as_namedtuple(thing) if :kwargs ∈ fieldnames(typeof(thing)) - if :kwargs ∉ keys(kwargs) - T(; struct_as_namedtuple(thing)..., thing.kwargs..., kwargs...) + if :args ∈ fieldnames(typeof(thing)) + named_thing = Base.structdiff(named_thing, (;args=())) + if :args ∉ keys(kwargs) + k = Base.structdiff(named_thing, (;args=())) + if :kwargs ∉ keys(kwargs) + T(; named_thing..., thing.kwargs..., kwargs...) + else + T(; named_thing..., kwargs[:kwargs]...) + end + else + kwargs2 = Base.structdiff((;kwargs...), (;args=())) + if :kwargs ∉ keys(kwargs) + T(kwargs[:args]...; named_thing..., thing.kwargs..., kwargs2...) + else + T(kwargs[:args]...; named_thing..., kwargs2[:kwargs]...) + end + end else - T(; struct_as_namedtuple(thing)..., kwargs[:kwargs]...) + if :kwargs ∉ keys(kwargs) + T(; named_thing..., thing.kwargs..., kwargs...) + else + T(; named_thing..., kwargs[:kwargs]...) + end end else - T(; struct_as_namedtuple(thing)..., kwargs...) + T(; named_thing..., kwargs...) end end diff --git a/test/remake_tests.jl b/test/remake_tests.jl index 447d3e876..0f8ea056d 100644 --- a/test/remake_tests.jl +++ b/test/remake_tests.jl @@ -284,3 +284,14 @@ newprob = remake(sdeprob; g = noise2!) @test newprob.f isa SDEFunction tmp = newprob.g([0.0, 0.0, 0.0], [1.0, 2.0, 3.0], nothing, 0.0) @test tmp≈[0.2, 0.4, 0.6] atol=1e-6 + +struct Remake_Test1 + p + args + kwargs +end +Remake_Test1(args...; p, kwargs...) = A(p, args, kwargs) +a = Remake_Test1(p=1) +@test remake(a, p=2) == Remake_Test1(p=2) +@test remake(a, args=1) == Remake_Test1(1, p=1) +@test remake(a, kwargs=(;a=1)) == Remake_Test1(p=1, a=1)