Skip to content

Commit

Permalink
Make unpacking a Tuple or NamedTuple Composite type-inferrable (#279)
Browse files Browse the repository at this point in the history
* Add breaking tests

* Impelement indexed_iterate for tuple composite

* Use hasfield if available

* Use Compat

* Make anonymous functions

* Increment version number

* Load hasfield from Compat
  • Loading branch information
sethaxen authored Jan 14, 2021
1 parent a202297 commit 14d746a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
name = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "0.9.25"
version = "0.9.26"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
BenchmarkTools = "0.5"
Compat = "2, 3"
FiniteDifferences = "0.10"
StaticArrays = "0.11, 0.12"
julia = "1"
Expand Down
1 change: 1 addition & 0 deletions src/ChainRulesCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module ChainRulesCore
using Base.Broadcast: broadcasted, Broadcasted, broadcastable, materialize, materialize!
using LinearAlgebra: LinearAlgebra
using SparseArrays: SparseVector, SparseMatrixCSC
using Compat: hasfield

export on_new_rule, refresh_rules # generation tools
export frule, rrule # core function
Expand Down
11 changes: 7 additions & 4 deletions src/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ Base.getindex(comp::Composite, idx) = getindex(backing(comp), idx)
# for Tuple
Base.getproperty(comp::Composite, idx::Int) = unthunk(getproperty(backing(comp), idx))
function Base.getproperty(
comp::Composite{P, <:NamedTuple{L}}, idx::Symbol
) where {P, L}
# Need to check L directly, or else this does not constant-fold
idx L || return Zero()
comp::Composite{P, T}, idx::Symbol
) where {P, T<:NamedTuple}
hasfield(T, idx) || return Zero()
return unthunk(getproperty(backing(comp), idx))
end

Expand All @@ -96,6 +95,10 @@ function Base.reverse(comp::Composite)
Composite{typeof(rev_backing), typeof(rev_backing)}(rev_backing)
end

function Base.indexed_iterate(comp::Composite{P,<:Tuple}, i::Int, state=1) where {P}
return Base.indexed_iterate(backing(comp), i, state)
end

function Base.map(f, comp::Composite{P, <:Tuple}) where P
vals::Tuple = map(f, backing(comp))
return Composite{P, typeof(vals)}(vals)
Expand Down
17 changes: 16 additions & 1 deletion test/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct StructWithInvariant
StructWithInvariant(x) = new(x, 2x)
end


@testset "Composite" begin
@testset "empty types" begin
@test typeof(Composite{Tuple{}}()) == Composite{Tuple{}, Tuple{}}
Expand Down Expand Up @@ -77,6 +76,22 @@ end
# Testing iterate via collect
@test collect(Composite{Foo}(x=2.5)) == [2.5]
@test collect(Composite{Tuple{Float64,}}(2.0)) == [2.0]

# Test indexed_iterate
ctup = Composite{Tuple{Float64,Int64}}(2.0, 3)
_unpack2tuple = function(comp)
a, b = comp
return (a, b)
end
@inferred _unpack2tuple(ctup)
@test _unpack2tuple(ctup) === (2.0, 3)

# Test getproperty is inferrable
_unpacknamedtuple = comp -> (comp.x, comp.y)
if VERSION v"1.2"
@inferred _unpacknamedtuple(Composite{Foo}(x=2, y=3.0))
@inferred _unpacknamedtuple(Composite{Foo}(y=3.0))
end
end

@testset "reverse" begin
Expand Down

2 comments on commit 14d746a

@sethaxen
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/27983

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.26 -m "<description of version>" 14d746a9eba54b3839cb9cb8bacedff304abfcc5
git push origin v0.9.26

Please sign in to comment.