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

define haskey/hasproperty on Composite #249

Merged
merged 3 commits into from
Nov 16, 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
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.17"
version = "0.9.18"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
5 changes: 5 additions & 0 deletions src/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ end
Base.keys(comp::Composite) = keys(backing(comp))
Base.propertynames(comp::Composite) = propertynames(backing(comp))

Base.haskey(comp::Composite, key) = haskey(backing(comp), key)
if isdefined(Base, :hasproperty)
Base.hasproperty(comp::Composite, key::Symbol) = hasproperty(backing(comp), key)
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)
Expand Down
8 changes: 8 additions & 0 deletions test/differentials/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ end
@testset "indexing, iterating, and properties" begin
@test keys(Composite{Foo}(x=2.5)) == (:x,)
@test propertynames(Composite{Foo}(x=2.5)) == (:x,)
@test haskey(Composite{Foo}(x=2.5), :x) == true
if isdefined(Base, :hasproperty)
@test hasproperty(Composite{Foo}(x=2.5), :y) == false
end
@test Composite{Foo}(x=2.5).x == 2.5

@test keys(Composite{Tuple{Float64,}}(2.0)) == Base.OneTo(1)
Expand All @@ -56,6 +60,10 @@ end
@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

# TODO: uncomment this once https://github.com/JuliaLang/julia/issues/35516
@test_broken haskey(Composite{Tuple{Float64}}(2.0), 1) == true
@test_broken hasproperty(Composite{Tuple{Float64}}(2.0), 2) == false

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

Expand Down