Skip to content

Commit e776799

Browse files
authored
Replace NO_FIELDS by NoTangent() (#163)
1 parent 8dc43d1 commit e776799

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/rand_tangent.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ function rand_tangent(rng::AbstractRNG, x::T) where {T}
3535
end
3636

3737
field_names = fieldnames(T)
38-
if length(field_names) > 0
39-
tangents = map(field_names) do field_name
40-
rand_tangent(rng, getfield(x, field_name))
41-
end
42-
if all(tangent isa NoTangent for tangent in tangents)
43-
# if none of my fields can be perturbed then I can't be perturbed
44-
return NoTangent()
45-
else
46-
Tangent{T}(; NamedTuple{field_names}(tangents)...)
47-
end
38+
tangents = map(field_names) do field_name
39+
rand_tangent(rng, getfield(x, field_name))
40+
end
41+
if all(tangent isa NoTangent for tangent in tangents)
42+
# if none of my fields can be perturbed then I can't be perturbed
43+
return NoTangent()
4844
else
49-
return NO_FIELDS
45+
Tangent{T}(; NamedTuple{field_names}(tangents)...)
5046
end
5147
end

test/rand_tangent.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ using FiniteDifferences: rand_tangent
4040
# structs.
4141
(Foo(5.0, 4, rand(rng, 3)), Tangent{Foo}),
4242
(Foo(4.0, 3, Foo(5.0, 2, 4)), Tangent{Foo}),
43-
(sin, typeof(NO_FIELDS)),
43+
(sin, NoTangent),
4444
# all fields NoTangent implies NoTangent
4545
(Pair(:a, "b"), NoTangent),
4646
(1:10, NoTangent),

0 commit comments

Comments
 (0)