Skip to content

Commit 786983c

Browse files
committed
make rand_tangent of struct with no pertable fields return DoesNotExist
1 parent 93b6058 commit 786983c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/rand_tangent.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ function rand_tangent(rng::AbstractRNG, x::T) where {T}
3333
tangents = map(field_names) do field_name
3434
rand_tangent(rng, getfield(x, field_name))
3535
end
36-
return Composite{T}(; NamedTuple{field_names}(tangents)...)
36+
if all(tangent isa DoesNotExist for tangent in tangents)
37+
# if none of my fields can be perturbed then I can't be perturbed
38+
return DoesNotExist()
39+
else
40+
Composite{T}(; NamedTuple{field_names}(tangents)...)
41+
end
3742
else
3843
return NO_FIELDS
3944
end

test/rand_tangent.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ using FiniteDifferences: rand_tangent
3232
((a=5.0, b=1), Composite{NamedTuple{(:a, :b), Tuple{Float64, Int}}}),
3333

3434
# structs.
35-
(sin, typeof(NO_FIELDS)),
3635
(Foo(5.0, 4, rand(rng, 3)), Composite{Foo}),
3736
(Foo(4.0, 3, Foo(5.0, 2, 4)), Composite{Foo}),
37+
(sin, typeof(NO_FIELDS)),
38+
# all fields DoesNotExist implies DoesNotExist
39+
(Pair(:a, "b"), DoesNotExist),
40+
(1:10, DoesNotExist),
41+
(1:2:10, DoesNotExist),
3842

3943
# LinearAlgebra types (also just structs).
4044
(

0 commit comments

Comments
 (0)