Skip to content

Commit 4a959b1

Browse files
Merge pull request #60 from Tokazama/master
New trait for collections that change sizes (issue #22)
2 parents 78b1e56 + 087d306 commit 4a959b1

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ serve as a staging ground for ideas before they merged into Base Julia. For this
1111
reason, no functionality is exported so that way if such functions are added
1212
and exported in a future Base Julia there will be no issues with the upgrade.
1313

14+
## parent_type(x)
15+
16+
Returns the parent array that `x` wraps.
17+
18+
## can_change_size(x)
19+
20+
Returns `true` if the size of `T` can change, in which case operations
21+
such as `pop!` and `popfirst!` are available for collections of type `T`.
22+
1423
## ismutable(x)
1524

1625
A trait function for whether `x` is a mutable or immutable array. Used for

src/ArrayInterface.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ parent_type(::Type{Symmetric{T,S}}) where {T,S} = S
2222
parent_type(::Type{<:LinearAlgebra.AbstractTriangular{T,S}}) where {T,S} = S
2323
parent_type(::Type{T}) where {T} = T
2424

25+
"""
26+
can_change_size(::Type{T}) -> Bool
27+
28+
Returns `true` if the size of `T` can change, in which case operations
29+
such as `pop!` and `popfirst!` are available for collections of type `T`.
30+
"""
31+
can_change_size(x) = can_change_size(typeof(x))
32+
can_change_size(::Type{T}) where {T} = false
33+
can_change_size(::Type{<:Vector}) = true
34+
can_change_size(::Type{<:AbstractDict}) = true
35+
can_change_size(::Type{<:Base.ImmutableDict}) = false
36+
2537
function ismutable end
2638

2739
"""

test/runtests.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,17 @@ end
182182

183183
@test isnothing(ArrayInterface.known_last(1:4))
184184
@test isnothing(ArrayInterface.known_last(typeof(1:4)))
185-
185+
186186
@test isnothing(ArrayInterface.known_step(typeof(1:0.2:4)))
187187
@test isone(ArrayInterface.known_step(1:4))
188188
@test isone(ArrayInterface.known_step(typeof(1:4)))
189189
end
190190

191+
@testset "can_change_size" begin
192+
@test ArrayInterface.can_change_size([1])
193+
@test ArrayInterface.can_change_size(Vector{Int})
194+
@test ArrayInterface.can_change_size(Dict{Symbol,Any})
195+
@test !ArrayInterface.can_change_size(Base.ImmutableDict{Symbol,Int64})
196+
@test !ArrayInterface.can_change_size(Tuple{})
197+
end
198+

0 commit comments

Comments
 (0)