From 1ae9796667a4e70cb2b4a048619c657160fb07a9 Mon Sep 17 00:00:00 2001 From: "Zachary P. Christensen" Date: Sat, 3 Sep 2022 18:20:52 -0400 Subject: [PATCH 1/5] field/property accessors for StaticSymbol --- src/Static.jl | 37 ++++++++++++++++++++++++++++--------- test/runtests.jl | 9 +++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Static.jl b/src/Static.jl index 5a557f7..bcc9925 100644 --- a/src/Static.jl +++ b/src/Static.jl @@ -437,15 +437,6 @@ Base.real(@nospecialize(x::StaticNumber)) = x Base.real(@nospecialize(T::Type{<:StaticNumber})) = eltype(T) Base.imag(@nospecialize(x::StaticNumber)) = zero(x) -""" - field_type(::Type{T}, f) - -Functionally equivalent to `fieldtype(T, f)` except `f` may be a static type. -""" -@inline field_type(T::Type, f::Union{Int, Symbol}) = fieldtype(T, f) -@inline field_type(::Type{T}, ::StaticInt{N}) where {T, N} = fieldtype(T, N) -@inline field_type(::Type{T}, ::StaticSymbol{S}) where {T, S} = fieldtype(T, S) - Base.rad2deg(::StaticFloat64{M}) where {M} = StaticFloat64(rad2deg(M)) Base.deg2rad(::StaticFloat64{M}) where {M} = StaticFloat64(deg2rad(M)) @generated Base.cbrt(::StaticFloat64{M}) where {M} = StaticFloat64(cbrt(M)) @@ -939,4 +930,32 @@ function Base.show(io::IO, m::MIME"text/plain", @nospecialize(x::NDIndex)) show(io, m, Tuple(x)) end +# field and property accessors +""" + field_type(::Type{T}, f) + +Functionally equivalent to `fieldtype(T, f)` except `f` may be a static type. +""" +@inline field_type(T::Type, f::Union{Int, Symbol}) = fieldtype(T, f) +@inline field_type(::Type{T}, ::StaticInt{N}) where {T, N} = fieldtype(T, N) +@inline field_type(::Type{T}, ::StaticSymbol{S}) where {T, S} = fieldtype(T, S) + +(::Base.Fix2{typeof(getfield),<:Union{StaticSymbol{f},StaticInt{f}}})(x) where {f} = getfield(x, f) +(::Base.Fix2{typeof(fieldtype),<:Union{StaticSymbol{f},StaticInt{f}}})(x) where {f} = fieldtype(x, f) + +Base.getproperty(x, ::StaticSymbol{S}) where {S} = getproperty(x, S) +Base.setproperty!(x, ::StaticSymbol{S}, v) where {S} = setproperty!(x, S, v) +Base.hasproperty(x, ::StaticSymbol{S}) where {S} = hasproperty(x, S) + +Base.getindex(nt::NamedTuple, ::StaticSymbol{S}) where {S} = getfield(nt, S) +function Base.getindex(nt::NamedTuple, idxs::Tuple{Vararg{<:StaticSymbol}}) + NamedTuple{known(idxs)}(nt) +end +function Base.setindex(nt::NamedTuple, v, ::StaticSymbol{S}) where {S} + merge(nt, NamedTuple{(S,)}((v,))) +end +function Base.setindex(nt::NamedTuple, vs, idxs::Tuple{Vararg{<:StaticSymbol}}) + merge(nt, NamedTuple{known(idxs)}((vs...,))) +end + end diff --git a/test/runtests.jl b/test/runtests.jl index 502ee35..9e14e74 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,6 +16,15 @@ Aqua.test_all(Static) @test @inferred(StaticSymbol(x, y, z)) === static(:xy1) @test @inferred(static(nothing)) === nothing @test_throws ErrorException static([]) + nt = (x = 1, y = 2) + @test nt[static(:x)] === + getproperty(nt, static(:x)) === + Base.Fix2(getfield, static(:x))(nt) === 1 + @test hasproperty(nt, static(:x)) + @test Base.setindex((a=1, b=2, c=3), 4, static(:b)) == + (a = 1, b = 4, c = 3) + @test Base.setindex((a=1, b=2, c=3), (4, 5), (static(:b), static(:d))) == + (a = 1, b = 4, c = 3, d = 5) end @testset "StaticInt" begin From 52427c1bf794f5a20c85b26be8f1c4cd6a54af5a Mon Sep 17 00:00:00 2001 From: "Zachary P. Christensen" Date: Sat, 3 Sep 2022 18:30:02 -0400 Subject: [PATCH 2/5] fix ambiguity --- src/Static.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Static.jl b/src/Static.jl index bcc9925..fd8a1f2 100644 --- a/src/Static.jl +++ b/src/Static.jl @@ -948,7 +948,7 @@ Base.setproperty!(x, ::StaticSymbol{S}, v) where {S} = setproperty!(x, S, v) Base.hasproperty(x, ::StaticSymbol{S}) where {S} = hasproperty(x, S) Base.getindex(nt::NamedTuple, ::StaticSymbol{S}) where {S} = getfield(nt, S) -function Base.getindex(nt::NamedTuple, idxs::Tuple{Vararg{<:StaticSymbol}}) +function Base.getindex(nt::NamedTuple, idxs::Tuple{<:StaticSymbol,Vararg{<:StaticSymbol}}) NamedTuple{known(idxs)}(nt) end function Base.setindex(nt::NamedTuple, v, ::StaticSymbol{S}) where {S} From 48c85a866f5873f16658275ac516549f6b3570bd Mon Sep 17 00:00:00 2001 From: Zachary P Christensen Date: Sat, 3 Sep 2022 18:38:56 -0400 Subject: [PATCH 3/5] Update src/Static.jl Co-authored-by: David Widmann --- src/Static.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Static.jl b/src/Static.jl index fd8a1f2..4ae07d8 100644 --- a/src/Static.jl +++ b/src/Static.jl @@ -954,7 +954,7 @@ end function Base.setindex(nt::NamedTuple, v, ::StaticSymbol{S}) where {S} merge(nt, NamedTuple{(S,)}((v,))) end -function Base.setindex(nt::NamedTuple, vs, idxs::Tuple{Vararg{<:StaticSymbol}}) +function Base.setindex(nt::NamedTuple, vs, idxs::Tuple{<:StaticSymbol,Vararg{<:StaticSymbol}}) merge(nt, NamedTuple{known(idxs)}((vs...,))) end From 4fa941e0b0ce0320a8f1dd07914567ab4e810a56 Mon Sep 17 00:00:00 2001 From: "Zachary P. Christensen" Date: Sat, 3 Sep 2022 20:30:28 -0400 Subject: [PATCH 4/5] format + more tests --- src/Static.jl | 15 +++++++++++---- test/runtests.jl | 22 +++++++++++++++------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Static.jl b/src/Static.jl index 4ae07d8..84f38fe 100644 --- a/src/Static.jl +++ b/src/Static.jl @@ -940,21 +940,28 @@ Functionally equivalent to `fieldtype(T, f)` except `f` may be a static type. @inline field_type(::Type{T}, ::StaticInt{N}) where {T, N} = fieldtype(T, N) @inline field_type(::Type{T}, ::StaticSymbol{S}) where {T, S} = fieldtype(T, S) -(::Base.Fix2{typeof(getfield),<:Union{StaticSymbol{f},StaticInt{f}}})(x) where {f} = getfield(x, f) -(::Base.Fix2{typeof(fieldtype),<:Union{StaticSymbol{f},StaticInt{f}}})(x) where {f} = fieldtype(x, f) +function (::Base.Fix2{typeof(getfield), <:Union{StaticSymbol{f}, StaticInt{f}}})(x) where {f + } + getfield(x, f) +end +function (::Base.Fix2{typeof(fieldtype), <:Union{StaticSymbol{f}, StaticInt{f}}})(x) where { + f + } + fieldtype(x, f) +end Base.getproperty(x, ::StaticSymbol{S}) where {S} = getproperty(x, S) Base.setproperty!(x, ::StaticSymbol{S}, v) where {S} = setproperty!(x, S, v) Base.hasproperty(x, ::StaticSymbol{S}) where {S} = hasproperty(x, S) Base.getindex(nt::NamedTuple, ::StaticSymbol{S}) where {S} = getfield(nt, S) -function Base.getindex(nt::NamedTuple, idxs::Tuple{<:StaticSymbol,Vararg{<:StaticSymbol}}) +function Base.getindex(nt::NamedTuple, idxs::Tuple{<:StaticSymbol, Vararg{<:StaticSymbol}}) NamedTuple{known(idxs)}(nt) end function Base.setindex(nt::NamedTuple, v, ::StaticSymbol{S}) where {S} merge(nt, NamedTuple{(S,)}((v,))) end -function Base.setindex(nt::NamedTuple, vs, idxs::Tuple{<:StaticSymbol,Vararg{<:StaticSymbol}}) +function Base.setindex(nt::NamedTuple, vs, idxs::Tuple{Vararg{<:StaticSymbol}}) merge(nt, NamedTuple{known(idxs)}((vs...,))) end diff --git a/test/runtests.jl b/test/runtests.jl index 9e14e74..346b828 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -17,14 +17,21 @@ Aqua.test_all(Static) @test @inferred(static(nothing)) === nothing @test_throws ErrorException static([]) nt = (x = 1, y = 2) - @test nt[static(:x)] === - getproperty(nt, static(:x)) === - Base.Fix2(getfield, static(:x))(nt) === 1 + @test nt[static(:x)] === 1 @test hasproperty(nt, static(:x)) - @test Base.setindex((a=1, b=2, c=3), 4, static(:b)) == - (a = 1, b = 4, c = 3) - @test Base.setindex((a=1, b=2, c=3), (4, 5), (static(:b), static(:d))) == - (a = 1, b = 4, c = 3, d = 5) + @test Base.setindex((a = 1, b = 2, c = 3), 4, static(:b)) == + (a = 1, b = 4, c = 3) + @test Base.setindex((a = 1, b = 2, c = 3), (4, 5), (static(:b), static(:d))) == + (a = 1, b = 4, c = 3, d = 5) + @test getindex((a = 1, b = 2, c = 3), (static(:b), static(:c))) == + (b = 2, c = 3) + mutable struct Foo + x::Int + end + f = Foo(3) + @test getproperty(f, static(:x)) === 3 + setproperty!(f, static(:x), 4) + @test Base.Fix2(getfield, static(:x))(f) === 4 end @testset "StaticInt" begin @@ -323,6 +330,7 @@ end @test @inferred(Static.permute(x, y)) === y @test @inferred(Static.eachop(getindex, x)) === x + @test Base.Fix2(fieldtype, static(:x))(typeof((x = 1, y = 2))) <: Int @test Static.field_type(typeof((x = 1, y = 2)), :x) <: Int @test Static.field_type(typeof((x = 1, y = 2)), static(:x)) <: Int function get_tuple_add(::Type{T}, ::Type{X}, dim::StaticInt) where {T, X} From ad58f521d97f5a0912ca5484c43217bcc3ee61ea Mon Sep 17 00:00:00 2001 From: "Zachary P. Christensen" Date: Mon, 5 Sep 2022 02:27:00 -0400 Subject: [PATCH 5/5] version bump --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3a2a4a3..e274282 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Static" uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3" authors = ["chriselrod", "ChrisRackauckas", "Tokazama"] -version = "0.7.6" +version = "0.7.7" [deps] IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"