Skip to content

Commit e8ac3a4

Browse files
author
BSnelling
authored
Merge pull request #185 from JuliaDiff/bes/diff_add_empty_tuples
Differential addition for empty tuples fix in composite.jl
2 parents e48b551 + a1b01fa commit e8ac3a4

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "0.9.2"
3+
version = "0.9.3"
44

55
[deps]
66
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"

src/differentials/composite.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ function Composite{P}(args...) where P
3636
return Composite{P, typeof(args)}(args)
3737
end
3838

39+
function Composite{P}() where P<:Tuple
40+
backing = ()
41+
return Composite{P, typeof(backing)}(backing)
42+
end
43+
3944
function Base.show(io::IO, comp::Composite{P}) where P
4045
print(io, "Composite{")
4146
show(io, P)

test/differentials/composite.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ end
2020

2121

2222
@testset "Composite" begin
23+
@testset "empty types" begin
24+
@test typeof(Composite{Tuple{}}()) == Composite{Tuple{}, Tuple{}}
25+
end
2326
@testset "convert" begin
2427
@test convert(NamedTuple, Composite{Foo}(x=2.5)) == (; x=2.5)
2528
@test convert(Tuple, Composite{Tuple{Float64,}}(2.0)) == (2.0,)
@@ -88,6 +91,10 @@ end
8891
end
8992

9093
@testset "Tuples" begin
94+
@test ==(
95+
typeof(Composite{Tuple{}}() + Composite{Tuple{}}()),
96+
Composite{Tuple{}, Tuple{}}
97+
)
9198
@test (
9299
Composite{Tuple{Float64, Float64}}(1.0, 2.0) +
93100
Composite{Tuple{Float64, Float64}}(1.0, 1.0)
@@ -113,6 +120,7 @@ end
113120
end
114121

115122
@testset "Tuples" begin
123+
@test Composite{Tuple{}}() + () == ()
116124
@test ((1.0, 2.0) + Composite{Tuple{Float64, Float64}}(1.0, 1.0)) == (2.0, 3.0)
117125
@test (Composite{Tuple{Float64, Float64}}(1.0, 1.0)) + (1.0, 2.0) == (2.0, 3.0)
118126
end

0 commit comments

Comments
 (0)