# MWE - setindex bug?

**URL:** <https://discourse.julialang.org/t/mwe-setindex-bug/18017>\
**Category:** General Usage\
**Created:** [November 26, 2018, 3:10pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017 "2018-11-26T15:10:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 26, 2018, 3:10pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/1 "2018-11-26T15:10:36Z")

</div>

The following line doesn’t error but doesn’t have any affect on b2

```julia
b2[vec(b2)][p2[xhalt*2:end] ] .= false;

```

Working in the same scope:

```julia
b3 = @view b2[vec(b2)];
b3[p2[rmax+1:end] ] .= false;

```

b2 is a bitvector, p2 a vector of ints  
Repeatable

```julia
b2 = trues(50); p2 = randperm(50);
b2[b2][p2[20:end]] .= false;
> sum(b2)
50
```

---

<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:** [November 26, 2018, 3:19pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/2 "2018-11-26T15:19:16Z")

</div>

Please provide a self-contained MWE, with `b2` etc defined.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 26, 2018, 3:33pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/3 "2018-11-26T15:33:28Z")

</div>

`b2[b2][p2] .= false ` , and  
`b2[p2][p2] .= false` fair the same aswell

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2018, 3:36pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/4 "2018-11-26T15:36:11Z")

</div>

> [@y4lu](#):
>
> b3 = @view b2[vec(b2)];

`b2[vec(b2)]` does not create a view so the behavior is expected to be different.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 26, 2018, 3:52pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/5 "2018-11-26T15:52:36Z")

</div>

It seems to be the second set of square brackets causing the trouble though, the `b3` part is a work around  
I wouldn’t have expected that much difference ;D

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2018, 4:01pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/6 "2018-11-26T16:01:48Z")

</div>

> [@y4lu](#):
>
> It seems to be the second set of square brackets causing the trouble though

No. It’s not. The first is `getindex` and only the second is `setindex!`. The original code is equivalent to `b3 = b2[vec(b2)]; b3[...]`.

> [@y4lu](#):
>
> I wouldn’t have expected that much difference

Difference between? Adding the `b3` shouldn’t make a difference and doesn’t. The difference is when you add `@view` and that’s exactly what it is meant to do.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 26, 2018, 5:53pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/7 "2018-11-26T17:53:54Z")

</div>

The key is that the syntaxes

```julia
A[I] = X
A[I] .= X

```

modify `A`. In your case, `A = (b2[vec(b2)])` — which is a copy and thus doesn’t modify the original `b2`. That’s why the view works, since views propagate their changes back to the original object.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 26, 2018, 11:23pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/8 "2018-11-26T23:23:15Z")

</div>

> [@yuyichao](#):
>
> Difference between?

behaviours of get/setindex on the left hand side.  
I think it’s a little confusing to call the first square brackets at fault because it would work without the second set, eg, `b2[b2] .= false` should be fine, but i guess that is a technical answer - how julia uses getindex and setindex in this case

So, is the current behaviour intended?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2018, 11:46pm UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/9 "2018-11-26T23:46:23Z")

</div>

> [@y4lu](#):
>
> So, is the current behaviour intended?

Yes.

> [@y4lu](#):
>
> behaviours of get/setindex on the left hand side.

So you mean getindex and setindex! should behave the same? I can take a few guesses why you might think that way (if they are wrong, please ignore…):

- If you are from python (or other language that returns view from indexing by default) then the short answer is that it’s been considered but it’s breaking enough and the performance of operations on views are bad enough that we believe it’s better to be explicit when views are required. (The performance issue isn’t a problem for python since it was slow anyway).
- If you are from C++ (or other languages that uses getindex to implement setindex!) then julia has a completely different object model and you’ll get a lot more related surprises.

> [@y4lu](#):
>
> I think it’s a little confusing to call the first square brackets at fault because it would work without the second set, eg, `b2[b2] .= false` should be fine

Well, you were never comparing the two. For the original code, the `getindex`, i.e. the first bracket, is definitely what gives you the behavior you didn’t expect. Now, if you were comparing it to something that you never mentioned, then sure, adding the second one could be how you get from a working version to this version. There are just so many changes that you could’ve made though, so it’s impossible for others to guess what was the last change you made. FWIW, removing the first square bracket will also make it “work”.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 27, 2018, 12:07am UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/10 "2018-11-27T00:07:40Z")

</div>

I did look at learning python a while back, but they used the phrase “syntactic sugar” too many times in the first video lecture 😉  
Technically i’d be a former octave user, though i only used it a little, enough to pass a few MOOCs  
Naively i would’ve thought all indexing on the left would be or reduce to setindexes  
tbh I’m not sure if that’s fair use of the ‘moving the goalposts’ concept but point taken - I say this because it seems very much stating the obvious that it works with just the one set of brackets

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 27, 2018, 1:22am UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/11 "2018-11-27T01:22:43Z")

</div>

Is there any chance that we could have this behaviour changed at some point, specifically for the left-hand side? The work around isn’t too bad but I at least would find the double-indexing set method quite convenient at times

Maybe a diy job I guess, it shouldn’t be too hard to add a method to setindex! make a function to combine indexes

---

<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:** [November 27, 2018, 1:43am UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/12 "2018-11-27T01:43:09Z")

</div>

> [@y4lu](#):
>
> Is there any chance that we could have this behaviour changed at some point, specifically for the left-hand side?

That is extremely unlikely (so unlikely that I don’t think it is worth spending too much time on it).

Also, a good default approach when you learn a language is to give the existing implementation a bit more of the benefit of the doubt and just blindly accept that things are the way they are for some reason. After a while, when you are more familiar with things, you might see that the behavior actually made sense. If it still feels odd, and then voicing concerns might be reasonable.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 27, 2018, 2:03am UTC](https://discourse.julialang.org/t/mwe-setindex-bug/18017/13 "2018-11-27T02:03:27Z")

</div>

You may consider my concerns voiced, reasonable or otherwise =)  
I shan’t ask for the sense of the behaviour  
add /spoiler: some [prior art](https://www.kaggle.com/n3n77i/kernels)
