Skip to content

Commit

Permalink
Merge pull request #60 from Tokazama/master
Browse files Browse the repository at this point in the history
New trait for collections that change sizes (issue #22)
  • Loading branch information
ChrisRackauckas authored Aug 12, 2020
2 parents 78b1e56 + 087d306 commit 4a959b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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

0 comments on commit 4a959b1

Please sign in to comment.