Skip to content

Commit ea4d68c

Browse files
authored
Merge pull request #176 from jw3126/phg/slicing
2 parents 94e905c + 434e7e0 commit ea4d68c

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Setfield"
22
uuid = "efcf1570-3423-57d1-acb7-fd33fddbac46"
3-
version = "1.1.0"
3+
version = "1.1.1"
44

55
[deps]
66
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"

src/setindex.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ Base.@propagate_inbounds function setindex(args...)
33
end
44

55
Base.@propagate_inbounds function setindex(xs::AbstractArray, v, I...)
6-
T = promote_type(eltype(xs), typeof(v))
6+
# we need to distinguish between scalar and sliced assignment
7+
I_normalized = Base.to_indices(xs, I)
8+
T = promote_type(eltype(xs), I_normalized isa Tuple{Vararg{Integer}} ? typeof(v) : eltype(v))
79
ys = similar(xs, T)
810
if eltype(xs) !== Union{}
911
copy!(ys, xs)
1012
end
11-
ys[I...] = v
13+
ys[I_normalized...] = v
1214
return ys
1315
end
1416

test/test_setindex.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ end
2323
@test @set(arr[1] = 10) == [10, 2, 3]
2424
@test arr == [1,2,3]
2525
@test Setfield.setindex(arr, 10.0, 1) ==ₜ Float64[10.0, 2.0, 3.0]
26+
@test Setfield.setindex(ones(2, 2), zeros(2), 1, :) ==ₜ Float64[0.0 0.0; 1.0 1.0]
27+
@test Setfield.setindex(ones(BigInt, 2, 2), zeros(Float32, 2), 1, :) ==ₜ BigFloat[0.0 0.0; 1.0 1.0]
28+
@test Setfield.setindex(fill(ones(1), 2, 2), [im, im], :, 1) ==hcat([im, im], [[1.0], [1.0]])
2629

2730
d = Dict(:a => 1, :b => 2)
2831
@test_throws MethodError Base.setindex(d, 10, :a)

0 commit comments

Comments
 (0)