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

New trait for collections that change sizes (issue #22) #60

Merged
merged 3 commits into from
Aug 12, 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/ArrayInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

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