Skip to content

Commit fe8e4fd

Browse files
authored
Make setindex! return the array rather than the value (#832)
1 parent 632dde0 commit fe8e4fd

File tree

7 files changed

+20
-8
lines changed

7 files changed

+20
-8
lines changed

src/FieldArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ abstract type FieldVector{N, T} <: FieldArray{Tuple{N}, T, 1} end
118118
end
119119

120120
@propagate_inbounds getindex(a::FieldArray, i::Int) = getfield(a, i)
121-
@propagate_inbounds setindex!(a::FieldArray, x, i::Int) = setfield!(a, i, x)
121+
@propagate_inbounds setindex!(a::FieldArray, x, i::Int) = (setfield!(a, i, x); a)
122122

123123
Base.cconvert(::Type{<:Ptr}, a::FieldArray) = Base.RefValue(a)
124124
Base.unsafe_convert(::Type{Ptr{T}}, m::Base.RefValue{FA}) where {N,T,D,FA<:FieldArray{N,T,D}} =

src/MArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ end
9999
error("setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.")
100100
end
101101

102-
return val
102+
return v
103103
end
104104

105105
@inline Tuple(v::MArray) = v.data

src/indexing.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ end
4040
@propagate_inbounds function setindex!(a::StaticArray, value, inds::Int...)
4141
@boundscheck checkbounds(a, inds...)
4242
_setindex!_scalar(Size(a), a, value, inds...)
43+
return a
4344
end
4445

4546
@generated function _setindex!_scalar(::Size{S}, a::StaticArray, value, inds::Int...) where S
@@ -126,14 +127,15 @@ end
126127

127128
@inline function setindex!(a::StaticArray, v, ::Colon)
128129
_setindex!(a::StaticArray, v, Length(a), :)
129-
return v
130+
return a
130131
end
131132

132133
@generated function _setindex!(a::StaticArray, v, ::Length{L}, ::Colon) where {L}
133134
exprs = [:(a[$i] = v) for i = 1:L]
134135
return quote
135136
@_inline_meta
136137
@inbounds $(Expr(:block, exprs...))
138+
return a
137139
end
138140
end
139141

@@ -145,6 +147,7 @@ end
145147
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination"))
146148
end
147149
@inbounds $(Expr(:block, exprs...))
150+
return a
148151
end
149152
end
150153

@@ -156,19 +159,21 @@ end
156159
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination"))
157160
end
158161
$(Expr(:block, exprs...))
162+
return a
159163
end
160164
end
161165

162166
@propagate_inbounds function setindex!(a::StaticArray, v, inds::StaticArray{<:Tuple, Int})
163167
_setindex!(a, v, Size(inds), inds)
164-
return v
168+
return a
165169
end
166170

167171
@generated function _setindex!(a::StaticArray, v, s::Size{S}, inds::StaticArray{<:Tuple, Int}) where {S}
168172
exprs = [:(a[inds[$i]] = v) for i = 1:prod(S)]
169173
return quote
170174
@_propagate_inbounds_meta
171175
similar_type(a, s)(tuple($(exprs...)))
176+
return a
172177
end
173178
end
174179

@@ -180,6 +185,7 @@ end
180185
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$(length(inds)) destination"))
181186
end
182187
$(Expr(:block, exprs...))
188+
return a
183189
end
184190
end
185191

@@ -191,6 +197,7 @@ end
191197
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$(length(inds)) destination"))
192198
end
193199
$(Expr(:block, exprs...))
200+
return a
194201
end
195202
end
196203

@@ -305,7 +312,7 @@ end
305312
quote
306313
@_propagate_inbounds_meta
307314
$(exprs...)
308-
return value
315+
return a
309316
end
310317
end
311318

@@ -348,7 +355,7 @@ end
348355
quote
349356
@_propagate_inbounds_meta
350357
$(exprs...)
351-
return v
358+
return a
352359
end
353360
else
354361
quote
@@ -358,7 +365,7 @@ end
358365
throw(DimensionMismatch("tried to assign $(length(v))-element array to $newsize destination"))
359366
end
360367
$(exprs...)
361-
return v
368+
return a
362369
end
363370
end
364371
end

src/util.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ end
7777

7878
size(a::TrivialView) = size(a.a)
7979
getindex(a::TrivialView, inds...) = getindex(a.a, inds...)
80-
setindex!(a::TrivialView, inds...) = setindex!(a.a, inds...)
80+
setindex!(a::TrivialView, inds...) = (setindex!(a.a, inds...); a)
8181
Base.IndexStyle(::Type{<:TrivialView{A}}) where {A} = IndexStyle(A)
8282

8383
TrivialView(a::AbstractArray{T,N}) where {T,N} = TrivialView{typeof(a),T,N}(a)

test/FieldMatrix.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464

6565
p = Tensor2x2(0.0, 0.0, 0.0, 0.0)
6666
p[1,1] = 1.0
67+
@test setindex!(p, 1.0, 1,1) === p
6768
p[2,1] = 2.0
6869

6970
@test size(p) === (2,2)

test/MArray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@
152152
v[2] = 12
153153
v[3] = 13
154154
@test v.data === (11, 12, 13)
155+
@test setindex!(v, 11, 1) === v
155156

156157
m = @MArray [0 0; 0 0]
157158
m[1] = 11
158159
m[2] = 12
159160
m[3] = 13
160161
m[4] = 14
161162
@test m.data === (11, 12, 13, 14)
163+
@test setindex!(m, 11, 1, 1) === m
162164

163165
@test_throws BoundsError setindex!(v, 4, -1)
164166
mm = @MArray zeros(3,3,3,3)

test/indexing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using StaticArrays, Test
3434
# SVector
3535
mv = MVector{4,Int}(undef)
3636
@test (mv[SVector(1,2,3,4)] = vec; (@inferred getindex(mv, SVector(4,3,2,1)))::MVector{4,Int} == MVector((7,6,5,4)))
37+
@test setindex!(mv, vec, SVector(1,2,3,4)) === mv
3738

3839
mv = MVector{4,Int}(undef)
3940
@test (mv[SVector(1,2,3,4)] = [4, 5, 6, 7]; (@inferred getindex(mv, SVector(4,3,2,1)))::MVector{4,Int} == MVector((7,6,5,4)))
@@ -53,6 +54,7 @@ using StaticArrays, Test
5354
@test (mv[:] = vec; (@inferred getindex(mv, :))::MVector{4,Int} == MVector((4,5,6,7)))
5455
@test (mv[:] = [4, 5, 6, 7]; (@inferred getindex(mv, :))::MVector{4,Int} == MVector((4,5,6,7)))
5556
@test (mv[:] = 2; (@inferred getindex(mv, :))::MVector{4,Int} == MVector((2,2,2,2)))
57+
@test setindex!(mv, 2, :) === mv
5658

5759
@test_throws DimensionMismatch setindex!(mv, SVector(1,2,3), SVector(1,2,3,4))
5860
@test_throws DimensionMismatch setindex!(mv, SVector(1,2,3), :)

0 commit comments

Comments
 (0)