diff --git a/README.md b/README.md index 1f0bc7c2b..d7c293ce7 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,15 @@ serve as a staging ground for ideas before they merged into Base Julia. For this reason, no functionality is exported so that way if such functions are added and exported in a future Base Julia there will be no issues with the upgrade. +## parent_type(x) + +Returns the parent array that `x` wraps. + +## can_change_size(x) + +Returns `true` if the size of `T` can change, in which case operations +such as `pop!` and `popfirst!` are available for collections of type `T`. + ## ismutable(x) A trait function for whether `x` is a mutable or immutable array. Used for diff --git a/src/ArrayInterface.jl b/src/ArrayInterface.jl index 385333c8b..85b601e78 100644 --- a/src/ArrayInterface.jl +++ b/src/ArrayInterface.jl @@ -22,6 +22,18 @@ parent_type(::Type{Symmetric{T,S}}) where {T,S} = S parent_type(::Type{<:LinearAlgebra.AbstractTriangular{T,S}}) where {T,S} = S parent_type(::Type{T}) where {T} = T +""" + can_change_size(::Type{T}) -> Bool + +Returns `true` if the size of `T` can change, in which case operations +such as `pop!` and `popfirst!` are available for collections of type `T`. +""" +can_change_size(x) = can_change_size(typeof(x)) +can_change_size(::Type{T}) where {T} = false +can_change_size(::Type{<:Vector}) = true +can_change_size(::Type{<:AbstractDict}) = true +can_change_size(::Type{<:Base.ImmutableDict}) = false + function ismutable end """ diff --git a/test/runtests.jl b/test/runtests.jl index a8f40d96e..f25fae932 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -182,9 +182,17 @@ end @test isnothing(ArrayInterface.known_last(1:4)) @test isnothing(ArrayInterface.known_last(typeof(1:4))) - + @test isnothing(ArrayInterface.known_step(typeof(1:0.2:4))) @test isone(ArrayInterface.known_step(1:4)) @test isone(ArrayInterface.known_step(typeof(1:4))) end +@testset "can_change_size" begin + @test ArrayInterface.can_change_size([1]) + @test ArrayInterface.can_change_size(Vector{Int}) + @test ArrayInterface.can_change_size(Dict{Symbol,Any}) + @test !ArrayInterface.can_change_size(Base.ImmutableDict{Symbol,Int64}) + @test !ArrayInterface.can_change_size(Tuple{}) +end +