Julia: strange error for deprecated scalar-fill indexed assignment

Created on 19 Sep 2018  Â·  8Comments  Â·  Source: JuliaLang/julia

With the deprecation removed, attempting a[idxs] = scalar gives an unclear error:

julia> a = fill(1, 10);

julia> a[1:5] = 0
ERROR: MethodError: no method matching setindex_shape_check(::Int64, ::Int64)
Closest candidates are:
  setindex_shape_check(::AbstractArray{#s57,1} where #s57, ::Integer) at indices.jl:218
  setindex_shape_check(::AbstractArray{#s57,1} where #s57, ::Integer, ::Integer) at indices.jl:221
  setindex_shape_check(::AbstractArray{#s57,2} where #s57, ::Integer, ::Integer) at indices.jl:225
  ...
Stacktrace:
 [1] macro expansion at ./multidimensional.jl:641 [inlined]
 [2] _unsafe_setindex!(::IndexLinear, ::Array{Int64,1}, ::Int64, ::UnitRange{Int64}) at ./multidimensional.jl:636
 [3] _setindex! at ./multidimensional.jl:631 [inlined]
 [4] setindex!(::Array{Int64,1}, ::Int64, ::UnitRange{Int64}) at ./abstractarray.jl:998
 [5] top-level scope at none:0
arrays error handling

Most helpful comment

I agree that a better error message would be helpful here. I imagine this will be a common novice mistake. Suggesting broadcast operators would give the user a cue.

All 8 comments

It also seems that the .= approach, while intuitive if you know about it, is not particularly well documented. Should probably be included in the indexing and assignment section of the arrays page?

I'm wondering what is the status of this problem. We also encountered this problem. Definitely, it is a good idea to include more explanation about indexing ans assignment. Many people want to do something like:

julia> x=randn(10);
julia> x[x .> 0.0] = 0.0
ERROR: MethodError: no method matching setindex_shape_check(::Float64, ::Int64)
Closest candidates are:
  setindex_shape_check(::AbstractArray{#s57,1} where #s57, ::Integer) at indices.jl:218
  setindex_shape_check(::AbstractArray{#s57,1} where #s57, ::Integer, ::Integer) at indices.jl:221
  setindex_shape_check(::AbstractArray{#s57,2} where #s57, ::Integer, ::Integer) at indices.jl:225
  ...
Stacktrace:
 [1] macro expansion at ./multidimensional.jl:641 [inlined]
 [2] _unsafe_setindex!(::IndexLinear, ::Array{Float64,1}, ::Float64, ::Base.LogicalIndex{Int64,BitArray{1}}) at ./multidimensional.jl:636
 [3] _setindex! at ./multidimensional.jl:631 [inlined]
 [4] setindex!(::Array{Float64,1}, ::Float64, ::BitArray{1}) at ./abstractarray.jl:998
 [5] top-level scope at none:0

Of course, if I replace the scalar 0.0 by a proper size array, e.g., zeros(sum(x .> 0.0)), i.e., then it works, but it's cumbersome really:

julia> x[x .> 0.0] = zeros(sum(x .> 0.0))
4-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
julia> x'
x'
1×10 Adjoint{Float64,Array{Float64,1}}:
 0.0  0.0  0.0  -0.565677  -1.90141  -0.441064  0.0  -0.305993  -0.178488  -1.72595

It would be really nice if this scalar assignment works as before in Julia v0.6.x and 0.7.

You are supposed to use x[x .> 0.0] .= 0.0.

@fredrikekre ; thank you very much for your clarification!

I agree that a better error message would be helpful here. I imagine this will be a common novice mistake. Suggesting broadcast operators would give the user a cue.

I've just hit this problem while migrating a part of my code from Julia 0.6 to 1.0 (for now, 1.0.1), and it took me a few hours to track it down. A better error message would be much welcome!

I would be willing to propose a PR for this, but I have no idea how you could implement it…

That'd be great @dourouc05! I _think_ it might be a simple as defining setindex_shape_check(::Any...) = throw(ArgumentError("better message"))). You can try it interactively with:

julia> @eval Base setindex_shape_check(::Any...) = throw(ArgumentError("nice string"))
setindex_shape_check (generic function with 7 methods)

julia> a = fill(1, 10);

julia> a[1:5] = 0
ERROR: ArgumentError: better message

Make the message better, test more cases, add those cases as tests, and you'll be on your way!

@mbauman : I just did this with #31085. What would be the next steps?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tkoolen picture tkoolen  Â·  3Comments

manor picture manor  Â·  3Comments

i-apellaniz picture i-apellaniz  Â·  3Comments

StefanKarpinski picture StefanKarpinski  Â·  3Comments

dpsanders picture dpsanders  Â·  3Comments