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

field/property accessors for StaticSymbol #83

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
37 changes: 28 additions & 9 deletions src/Static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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{<:StaticSymbol,Vararg{<:StaticSymbol}})
NamedTuple{known(idxs)}(nt)
end
function Base.setindex(nt::NamedTuple, v, ::StaticSymbol{S}) where {S}
merge(nt, NamedTuple{(S,)}((v,)))
Copy link
Contributor

@cscherrer cscherrer Sep 5, 2022

Choose a reason for hiding this comment

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

For named tuples, Base.merge is not type-stable for nested structures. It uses if @generated, which seems to allow the compiler to give up very early on type inference.

I've solved this in my packages by defining an alternate function to use instead of merge that's always generated (so no type-unstable fallback). I'd file an issue about this, but I so often see compiler speed favored over strong type inference, so I'm not sure it would be well-received.

Just mentioning in case that could also be an issue here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you have a mwe example where this fails so I can have a test that avoids this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also worth considering: Should this be

function Base.setindex(nt::NamedTuple{N,T}, v, ::StaticSymbol{S}) where {S,N,T}

?

That would help it to specialize for the NamedTuple, which may help merge instability be a non-issue. Details here in the Julia docs.

The tricky thing with all of this is that the compiler makes so few guarantees. Type stability is very context-dependent, even depending on what's been compiled so far. So code that seems fine when I write it can have subtle problems downstream.

Sorry this comment is so long. As might be obvious, I think there are lots of open questions around this kind of thing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have a mwe example where this fails so I can have a test that avoids this?

No, I guess it's more of a "this might bite us". But then again, it may not. That's the problem with lack of compiler guarantees :)

Maybe the best way to handle this is to leave the code as-is, but add a comment to make it easier later in case this becomes a problem. I think I'd point to the specialization link in the docs and mention the fact that if @generated in Base.merge can lead to type instability.

end
function Base.setindex(nt::NamedTuple, vs, idxs::Tuple{Vararg{<:StaticSymbol}})
merge(nt, NamedTuple{known(idxs)}((vs...,)))
end

end
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down