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

setindex! returns the wrong value #827

Closed
KristofferC opened this issue Sep 24, 2020 · 5 comments · Fixed by #832
Closed

setindex! returns the wrong value #827

KristofferC opened this issue Sep 24, 2020 · 5 comments · Fixed by #832
Labels

Comments

@KristofferC
Copy link
Contributor

julia> a = rand(MVector{3});

julia> (c = setindex!(a, 1, 1)); c
1

julia> (c = setindex!(collect(a), 1, 1)); c
3-element Array{Float64,1}:
 1.0
 0.30794928630799046
 0.6842053878737089
@c42f c42f added the bug label Sep 25, 2020
@c42f
Copy link
Member

c42f commented Sep 26, 2020

Is there an official API for the return value of setindex!? I couldn't find it at https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array, and indeed the examples there seem to return the newly set value rather than the array. setfield! also returns the field value rather than the mutated object.

On the other hand, Base.Array behaves as you say and seems to be consistent about it. So I'm very happy to change this, though also confused about whether there's a "correct" option.

@tkf
Copy link
Member

tkf commented Sep 26, 2020

There was a similar discussion in JuliaLang/julia#31891

It looks like the decision was that setindex! can return anything since the return value of (x[i] = v) is determined by lowering.

@tkf
Copy link
Member

tkf commented Sep 26, 2020

(Though I think it would have been great if setindex!(A, ...) === A was the part of the API.)

@KristofferC
Copy link
Contributor Author

Is there an official API for the return value of setindex!?

The convention is to return the modified object.

Some more examples:

julia> a = rand(1,1);

julia> (c = setindex!(a, 1, 1); c)
1×1 Array{Float64,2}:
 1.0

julia> (c = setindex!(sparse(a), 1, 1); c)
1×1 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [1, 1]  =  1.0

julia> (c = setindex!(view(a), 1, 1); c)
0-dimensional view(::Array{Float64,0}) with eltype Float64:
1.0

julia> (c = setindex!(MMatrix{1,1}(a), 1, 1); c)
1

@c42f
Copy link
Member

c42f commented Sep 28, 2020

Ok, let's follow what Array does. I made #832.

@c42f c42f closed this as completed in #832 Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants