Skip to content

Commit 77146e8

Browse files
authored
Add can_setindex methods (#251)
* Add `can_setindex` methods
1 parent 0d039cd commit 77146e8

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "5.0.3"
3+
version = "5.0.4"
44

55
[deps]
66
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

src/ArrayInterface.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import Compat
1313
using Base: @propagate_inbounds, tail, OneTo, LogicalIndex, Slice, ReinterpretArray,
1414
ReshapedArray, AbstractCartesianIndex
1515

16+
using Base.Iterators: Pairs
17+
1618
const CanonicalInt = Union{Int,StaticInt}
1719
canonicalize(x::Integer) = Int(x)
1820
canonicalize(@nospecialize(x::StaticInt)) = x
@@ -187,6 +189,11 @@ Query whether a type can use `setindex!`.
187189
can_setindex(x) = can_setindex(typeof(x))
188190
can_setindex(::Type) = true
189191
can_setindex(::Type{<:AbstractRange}) = false
192+
can_setindex(::Type{<:AbstractDict}) = true
193+
can_setindex(::Type{<:Base.ImmutableDict}) = false
194+
can_setindex(@nospecialize T::Type{<:Tuple}) = false
195+
can_setindex(@nospecialize T::Type{<:NamedTuple}) = false
196+
can_setindex(::Type{<:Pairs{<:Any,<:Any,P}}) where {P} = can_setindex(P)
190197

191198
"""
192199
aos_to_soa(x)

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ end
7272
@test !@inferred(ArrayInterface.can_setindex(1:2))
7373
@test @inferred(ArrayInterface.can_setindex(Vector{Int}))
7474
@test !@inferred(ArrayInterface.can_setindex(UnitRange{Int}))
75+
@test !@inferred(ArrayInterface.can_setindex(Base.ImmutableDict{Int,Int}))
76+
@test !@inferred(ArrayInterface.can_setindex(Tuple{}))
77+
@test !@inferred(ArrayInterface.can_setindex(NamedTuple{(),Tuple{}}))
78+
@test @inferred(ArrayInterface.can_setindex(Dict{Int,Int}))
7579
end
7680

7781
@testset "ArrayInterface.isstructured" begin

0 commit comments

Comments
 (0)