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

Document setindex! / push! calling convert #40498

Open
goretkin opened this issue Apr 15, 2021 · 2 comments
Open

Document setindex! / push! calling convert #40498

goretkin opened this issue Apr 15, 2021 · 2 comments

Comments

@goretkin
Copy link
Contributor

I thought it was documented that setindex! calls convert on the value, and in some cases the "key".

e.g. convert both key and value:

julia> d = Dict{Int, Complex{Float64}}()
Dict{Int64, ComplexF64}()

julia> d[1.0] = true
true

julia> d
Dict{Int64, ComplexF64} with 1 entry:
  1 => 1.0+0.0im

convert just value, not key:

julia> push!(Vector{ComplexF64}(), true)
1-element Vector{ComplexF64}:
 1.0 + 0.0im

julia> [1][1.0] = 3
ERROR: ArgumentError: invalid index: 1.0 of type Float64

Is it correct to document that setindex! and push! does conversion of the value? I wondered this here.

help?> setindex!
search: setindex!

  setindex!(collection, value, key...)

  Store the given value at the given key or index within a collection. The syntax a[i,j,...] = x is converted by the compiler to
  (setindex!(a, x, i, j, ...); x).

  ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  setindex!(A, X, inds...)
  A[inds...] = X

  Store values from array X within some subset of A as specified by inds. The syntax A[inds...] = X is equivalent to (setindex!(A, X,
  inds...); X).

@jakobnissen
Copy link
Contributor

It is documented in the Julia manual, under conversion and promotion.

One could argue it belongs in the docstrings, but I think in this case, it's more of a generic Julia behaviour thing, and less of a specific push! thing.

@goretkin
Copy link
Contributor Author

Thanks, @jakobnissen.

Assigning to an array converts to the array's element type.

That sounds like it should just be in the documentation for setindex!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants