Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FiniteDifferences"
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
version = "0.12.3"
version = "0.12.4"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
7 changes: 6 additions & 1 deletion src/rand_tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ function rand_tangent(rng::AbstractRNG, x::T) where {T}
tangents = map(field_names) do field_name
rand_tangent(rng, getfield(x, field_name))
end
return Composite{T}(; NamedTuple{field_names}(tangents)...)
if all(tangent isa DoesNotExist for tangent in tangents)
# if none of my fields can be perturbed then I can't be perturbed
return DoesNotExist()
else
Composite{T}(; NamedTuple{field_names}(tangents)...)
end
else
return NO_FIELDS
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The outer if here that gives us the NO_FIELDS would be able to be removed if we replaced NO_FIELDS with DoesNotExist().
Since then we would be in the case that all the fields (of which there are none) subtype DoesNotExist.
Vacuous truth.

JuliaDiff/ChainRulesCore.jl#175 (comment)

end
Expand Down
6 changes: 5 additions & 1 deletion test/rand_tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ using FiniteDifferences: rand_tangent
((a=5.0, b=1), Composite{NamedTuple{(:a, :b), Tuple{Float64, Int}}}),

# structs.
(sin, typeof(NO_FIELDS)),
(Foo(5.0, 4, rand(rng, 3)), Composite{Foo}),
(Foo(4.0, 3, Foo(5.0, 2, 4)), Composite{Foo}),
(sin, typeof(NO_FIELDS)),
# all fields DoesNotExist implies DoesNotExist
(Pair(:a, "b"), DoesNotExist),
(1:10, DoesNotExist),
(1:2:10, DoesNotExist),

# LinearAlgebra types (also just structs).
(
Expand Down