Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make getproperty on Composite unthunk #121

Merged
merged 2 commits into from
Oct 15, 2020
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
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