Skip to content

Commit

Permalink
fix remake when args is a field of the struct
Browse files Browse the repository at this point in the history
  • Loading branch information
oscardssmith committed Aug 23, 2024
1 parent 32f5308 commit 5ca0687
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/remake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions test/remake_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5ca0687

Please sign in to comment.