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 = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "0.9.21"
version = "0.9.22"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
4 changes: 4 additions & 0 deletions src/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ end
Base.iterate(comp::Composite, args...) = iterate(backing(comp), args...)
Base.length(comp::Composite) = length(backing(comp))
Base.eltype(::Type{<:Composite{<:Any, T}}) where T = eltype(T)
function Base.reverse(comp::Composite)
rev_backing = reverse(backing(comp))
Composite{typeof(rev_backing), typeof(rev_backing)}(rev_backing)
end

function Base.map(f, comp::Composite{P, <:Tuple}) where P
vals::Tuple = map(f, backing(comp))
Expand Down
14 changes: 13 additions & 1 deletion test/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ end
@test hash(Composite{Foo}(y=2.5, x=Zero())) == hash(Composite{Foo}(y=2.5))
end


@testset "indexing, iterating, and properties" begin
@test keys(Composite{Foo}(x=2.5)) == (:x,)
@test propertynames(Composite{Foo}(x=2.5)) == (:x,)
Expand Down Expand Up @@ -75,6 +74,19 @@ end
@test collect(Composite{Tuple{Float64,}}(2.0)) == [2.0]
end

@testset "reverse" begin
c = Composite{Tuple{Int, Int, String}}(1, 2, "something")
cr = Composite{Tuple{String, Int, Int}}("something", 2, 1)
@test reverse(c) === cr

# can't reverse a named tuple or a dict
@test_throws MethodError reverse(Composite{Foo}(;x=1.0, y=2.0))

d = Dict(:x => 1, :y => 2.0)
cdict = Composite{Foo, typeof(d)}(d)
@test_throws MethodError reverse(Composite{Foo}())
end

@testset "unset properties" begin
@test Composite{Foo}(; x=1.4).y === Zero()
end
Expand Down