# Argument order for setindex! and setproperty! not consistent

**URL:** https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293
**Category:** Internals & Design
**Created:** [July 12, 2019, 5:06pm UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293 "2019-07-12T17:06:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![dstarerstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dstarerstor/32/8958_2.png) [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)
#### Post date: [July 12, 2019, 5:06pm UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293/1 "2019-07-12T17:06:27Z")

</div>

This is really minor, but I happened to notice that the argument order is not consistent between `setindex!` and `setproperty!`. For example, the methods

```julia
setindex!(A::Array{Any,N} where N, x, i::Int64) in Base at essentials.jl:419
setproperty!(x::Type, f::Symbol, v) in Base at sysimg.jl:16

```

In `setindex!` the value comes before the index. In `setproperty!` the symbol comes before the value. Given that the symbol and the index are analogous, it would be nice if they were consistent (although changing either would likely break people’s code)

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 12, 2019, 7:22pm UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293/2 "2019-07-12T19:22:08Z")

</div>

I’ve always found the order of `setindex!` to be counter-intuitive, but I think there’s a good reason for it: `setindex!` can actually take multiple index arguments (for example, when you do `x[i, j, k] = 3` you are calling `setindex!(x, 3, i, j, j)`. Julia only allows variable numbers of arguments to happen at the _end_ of the list of arguments, so putting the variable number of indices at the end makes sense.

`setproperty!`, on the other hand, only takes exactly one property and one value, so it doesn’t have this issue. I agree that it’s unfortunate that they’re inconsistent, although I do find the order chosen for `setproperty!` to be more intuitive.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 13, 2019, 5:40am UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293/3 "2019-07-13T05:40:40Z")

</div>

I agree that it is a wart, and goes against the recommendation of the [style guide](https://docs.julialang.org/en/v1.3-dev/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia-Base-1) — the mutated argument should be the first here.

I don’t think there is an open issue, can you please open one? This can only be fixed in 2.0 but should not be forgotten.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 13, 2019, 6:49am UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293/4 "2019-07-13T06:49:31Z")

</div>

> [@Tamas\_Papp](#):
>
> I agree that it is a wart, and goes against the recommendation of the [style guide](https://docs.julialang.org/en/v1.3-dev/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia-Base-1) — the mutated argument should be the first here.

But it is?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 13, 2019, 7:50am UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293/5 "2019-07-13T07:50:38Z")

</div>

You are right — I did not think this through. It is `setproperty!` that is actually consistent with the style guide, and `setindex!` which (technically) isn’t, but @rdeits explained why this makes sense.

So I guess it is fine as it is.

---

<div class="post-metadata">

### Author: ![dstarerstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dstarerstor/32/8958_2.png) [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)
#### Post date: [July 13, 2019, 3:35pm UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293/6 "2019-07-13T15:35:49Z")

</div>

Maybe a solution would be to change `setproperty!` to mimic `setindex!` and allow varargs, defined recursively so that `setproperty!(x, "foo", :a, :b)` would call `setproperty!(x.a, "foo", :b)` which is called when you type `x.a.b = "foo"`

Probably not super useful, but it would lead to some consistency, similar to how `setindex!(x, "foo", a, b)` is equivalent to `setindex!(x[a,:], "foo", b)` for 2D arrays.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 14, 2019, 3:04am UTC](https://discourse.julialang.org/t/argument-order-for-setindex-and-setproperty-not-consistent/26293/7 "2019-07-14T03:04:18Z")

</div>

The way to make these consistent seems like it would be to always require a tuple of indices for `setindex!` but I think people would find that annoying too. Seems like the currrent arrangement is probably the best we can manage.
