Skip to content

Commit

Permalink
Make getproperty on Composite unthunk (#121)
Browse files Browse the repository at this point in the history
* Make `getproperty` on `Composite` unthunk

* Bump patch version
  • Loading branch information
nickrobinson251 authored Oct 15, 2020
1 parent 112184d commit a3e76b1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions 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.15"
version = "0.9.16"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -11,7 +11,7 @@ BenchmarkTools = "0.5"
FiniteDifferences = "0.10"
MuladdMacro = "0.2.1"
StaticArrays = "0.11, 0.12"
julia = "^1.0"
julia = "1"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand Down
5 changes: 3 additions & 2 deletions src/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ Base.convert(::Type{<:Tuple}, comp::Composite{<:Any, <:Tuple}) = backing(comp)
Base.convert(::Type{<:Dict}, comp::Composite{<:Dict, <:Dict}) = backing(comp)

Base.getindex(comp::Composite, idx) = getindex(backing(comp), idx)
Base.getproperty(comp::Composite, idx::Int) = getproperty(backing(comp), idx) # for Tuple

# 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()
return getproperty(backing(comp), idx)
return unthunk(getproperty(backing(comp), idx))
end

Base.keys(comp::Composite) = keys(backing(comp))
Expand Down
2 changes: 2 additions & 0 deletions test/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ end
@test keys(Composite{Tuple{Float64,}}(2.0)) == Base.OneTo(1)
@test propertynames(Composite{Tuple{Float64,}}(2.0)) == (1,)
@test getproperty(Composite{Tuple{Float64,}}(2.0), 1) == 2.0
@test getproperty(Composite{Tuple{Float64,}}(@thunk 2.0^2), 1) == 4.0
@test getproperty(Composite{Tuple{Float64,}}(a=(@thunk 2.0^2),), :a) == 4.0

@test length(Composite{Foo}(x=2.5)) == 1
@test length(Composite{Tuple{Float64,}}(2.0)) == 1
Expand Down

2 comments on commit a3e76b1

@nickrobinson251
Copy link
Contributor 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/22990

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.16 -m "<description of version>" a3e76b1eb0573dcde5e77c5dad8bed6e4d2dcb13
git push origin v0.9.16

Please sign in to comment.