Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strange error for deprecated scalar-fill indexed assignment #29266

Closed
JeffBezanson opened this issue Sep 19, 2018 · 8 comments
Closed

strange error for deprecated scalar-fill indexed assignment #29266

JeffBezanson opened this issue Sep 19, 2018 · 8 comments
Labels
domain:arrays [a, r, r, a, y, s] domain:error handling Handling of exceptions by Julia or the user

Comments

@JeffBezanson
Copy link
Sponsor Member

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
@JeffBezanson JeffBezanson added domain:arrays [a, r, r, a, y, s] domain:error handling Handling of exceptions by Julia or the user labels Sep 19, 2018
@acrosby
Copy link

acrosby commented Oct 17, 2018

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?

@BoundaryValueProblems
Copy link

BoundaryValueProblems commented Oct 26, 2018

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.

@fredrikekre
Copy link
Member

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

@BoundaryValueProblems
Copy link

@fredrikekre ; thank you very much for your clarification!

@BioTurboNick
Copy link
Contributor

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.

@dourouc05
Copy link
Contributor

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…

@mbauman
Copy link
Sponsor Member

mbauman commented Feb 15, 2019

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!

@dourouc05
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] domain:error handling Handling of exceptions by Julia or the user
Projects
None yet
Development

No branches or pull requests

7 participants