Skip to content

Commit 968d36d

Browse files
committed
Allow empty indexing of zero-dimensional MArrays
1 parent 786b6f0 commit 968d36d

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Scalar.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const Scalar{T} = SArray{Tuple{},T,0,1}
1414
end
1515
@inline convert(::Type{SA}, sa::SA) where {SA <: Scalar} = sa
1616

17-
getindex(v::Scalar) = v.data[1]
1817
@propagate_inbounds function getindex(v::Scalar, i::Int)
1918
@boundscheck if i != 1
2019
error("Attempt to index Scalar at index $i")

src/indexing.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ setindex!(a::StaticArray, value, i::Int) = error("setindex!(::$(typeof(a)), valu
1414
end
1515

1616
@generated function _getindex_scalar(::Size{S}, a::StaticArray, inds::Int...) where S
17+
if length(inds) == 0
18+
return quote
19+
@_propagate_inbounds_meta
20+
a[1]
21+
end
22+
end
23+
1724
stride = 1
1825
ind_expr = :()
1926
for i 1:length(inds)
@@ -36,6 +43,13 @@ end
3643
end
3744

3845
@generated function _setindex!_scalar(::Size{S}, a::StaticArray, value, inds::Int...) where S
46+
if length(inds) == 0
47+
return quote
48+
@_propagate_inbounds_meta
49+
a[1] = value
50+
end
51+
end
52+
3953
stride = 1
4054
ind_expr = :()
4155
for i 1:length(inds)

test/MArray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,11 @@
157157
@test @inferred(promote_type(MMatrix{2,3,Float32,6}, MMatrix{2,3,Complex{Float64},6})) === MMatrix{2,3,Complex{Float64},6}
158158
@test @inferred(promote_type(MArray{Tuple{2, 2, 2, 2},Float32, 4, 16}, MArray{Tuple{2, 2, 2, 2}, Complex{Float64}, 4, 16})) === MArray{Tuple{2, 2, 2, 2}, Complex{Float64}, 4, 16}
159159
end
160+
161+
@testset "zero-dimensional" begin
162+
v = MArray{Tuple{}, Int, 0, 1}(1)
163+
@test v[] == 1
164+
v[] = 2
165+
@test v[] == 2
166+
end
160167
end

0 commit comments

Comments
 (0)