# Why is the following behaviour not a bug?

**URL:** <https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169>\
**Category:** General Usage\
**Created:** [October 28, 2020, 11:00am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169 "2020-10-28T11:00:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [October 28, 2020, 11:00am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/1 "2020-10-28T11:00:35Z")

</div>

I just observed the following on v1.4 (so apologies if it is gone on v1.5).

```julia
julia> x = [rand(0:9, 2) for a = 1:3]
3-element Array{Array{Int64,1},1}:
 [3, 8]
 [2, 4]
 [3, 1]

julia> x[1,1]
2-element Array{Int64,1}:
 3
 8

julia> x[1]
2-element Array{Int64,1}:
 3
 8

```

Why does `x[1,1]` work at all, and given that it does work, why does it return the same thing as `x[1]`? And given that it does work, why does `x[1,2]` not work?

I’m sure there must be a reason but can’t for the life of me see what it is.

Cheers

Colin

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 28, 2020, 11:07am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/2 "2020-10-28T11:07:20Z")

</div>

There is a short note at the end of this chapter:  
[https://docs.julialang.org/en/v1/manual/arrays/#Omitted-and-extra-indices](https://docs.julialang.org/en/v1/manual/arrays/#Omitted-and-extra-indices)

You can in your example do:

```julia
julia> x[1,1,1,1,1]
2-element Array{Int64,1}:
 7
 3

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 28, 2020, 12:03pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/3 "2020-10-28T12:03:30Z")

</div>

> [@colintbowers](#):
>
> And given that it does work, why does `x[1,2]` not work?

It can’t work for `x[1,2]` since that would require `x` to be a matrix with at least two columns, which it isn’t. The fact that you can append arbitrary numbers of `1`s to the indices is indeed a bit confusing, given that there is a clear separation between vectors and matrices.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 28, 2020, 12:40pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/4 "2020-10-28T12:40:15Z")

</div>

> [@DNF](#):
>
> The fact that you can append arbitrary numbers of `1` s to the indices is indeed a bit confusing, given that there is a clear separation between vectors and matrices.

Quoted from the docs:

> Similarly, more than `N` indices may be provided if all the indices beyond the dimensionality of the array are `1` (or more generally are the first and only element of `axes(A, d)` where `d` is that particular dimension number).

This seems to be general enough to hold for every number of indices.

> This allows vectors to be indexed like one-column matrices, for example

This the example use case.

The clear separation of vectors and matrices is existent in Julia but actually this fact is what I wonder about. I would expect to have the multidimensional array as the base for everything, in other words the general concept. But of course this would be academic and would hinder for best performance and memory efficiency.  
So I see it this way: the gerenal concept is the multidimensional array and the pragmatic specializations are numbers (0-dim), vectors (1-dim) and matrices (2-dim). And in several packages there are more of these specializations.

Confusing? Only for mathematicians. 🙂

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 28, 2020, 12:47pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/5 "2020-10-28T12:47:25Z")

</div>

Yes, the docs are clear enough, but it clashes with my explanation when I try to tell people that vectors are different from an Nx1 matrix, saying that there is no second dimension at all.

> [@oheil](#):
>
> I would expect to have the multidimensional array as the base for everything, in other words the general concept. But of course this would be academic and would hinder for best performance and memory efficiency.

Maybe I misunderstand, but please, no. Vectors as Nx1 matrices is a terrible thing, in fact I think it may be my _least_ favorite thing with Matlab(!) It causes endless troubles, and I’m not talking about performance.

---

<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:** [October 28, 2020, 12:57pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/6 "2020-10-28T12:57:58Z")

</div>

> [@oheil](#):
>
> Confusing? Only for mathematicians.

This is a design choice that was taken very early in Julia and cannot be changed now as it would break the API, but with hindsight it is not so obvious that this was the best solution.

These things will be with us for a while, and may not be worth changing later on either, but it is OK to find them confusing. Julia, like every language, does have some warts, it is not necessary to rationalize them.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 28, 2020, 1:07pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/7 "2020-10-28T13:07:21Z")

</div>

Relax, nothing I want to change. It was only meant for the point of view on some things and what could confuse and what not. Of course, this is indivual taste and my taste of the point if view is first general multidimensional array and after that specialized implementations. So I am not confused about

```julia
julia> x[1,1,1,1,1]
2-element Array{Int64,1}:
 7
 3

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 28, 2020, 1:27pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/8 "2020-10-28T13:27:38Z")

</div>

> [@oheil](#):
>
> Of course, this is indivual taste and my taste of the point if view is first general multidimensional array and after that specialized implementations.

Since I spend so much time fuming over the lack of distinction between vectors and matrices in Matlab (it _is_ my day job), I’m curious if you can explain this a little more closely. What does this mean, and is it different from Julia, and similar to Matlab?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 28, 2020, 1:47pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/9 "2020-10-28T13:47:27Z")

</div>

> [@DNF](#):
>
> What does this mean, and is it different from Julia, and similar to Matlab?

This I can’t answer as I do not have any experience with matlab.

> [@DNF](#):
>
> Since I spend so much time fuming over the lack of distinction between vectors and matrices in Matlab (it _is_ my day job), I’m curious if you can explain this a little more closely.

For this it seems that you interprete too much into my remark. It was meant regarding to this statement quoted from docs:

> [@](#):
>
> Similarly, more than `N` indices may be provided if all the indices beyond the dimensionality of the array are `1` (or more generally are the first and only element of `axes(A, d)` where `d` is that particular dimension number).

As you find it confusing that `x[1,1,1,1,1,...addmore]` work with respect that vectors and matrices are separated concepts/implementations.

What I want to say is, when I read similar sentences in the docs my state of mind is, that I have a general NxNxNx…xN (multidimensional) array as a picture, and with this, above statement in the docs is in no way confusing. Thats all here.

I know, there are other languages, where it is different, where e.g. numbers are internally 0-dim arrays (is it haskell? I don’t know and it doesn’t matter, so I don’t look it up for now), and this may make sense for those languages, but of course, as @Tamas_Papp pointed out, it doesn’t make sense for Julia to have this type of academic stringency. And now I learned that Matlab seems not to separate 1-dim Arrays (vectors) from 2-dim arrays (matrices), may be it is not needed for Matlab to distinguish because the implementation (probably in C) would not benefit in terms of memory usage and performance.

Puh, many words about not so much I have to say about it ☹ But I hope, that OP understands why it isn’t a bug, but a feature.

---

<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:** [October 28, 2020, 1:50pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/10 "2020-10-28T13:50:19Z")

</div>

> [@oheil](#):
>
> as @Tamas_Papp pointed out, it doesn’t make sense for Julia to have this type of academic stringency

I think you misunderstood my post: I am arguing that supporting the trailing `1`s makes little sense with how Julia works currently. It is a historical artifact, it did make some algorithms easier initially.

I would prefer catching `x::Vector[1,1]` etc as an error. But, again, that would break the API now.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 28, 2020, 1:52pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/11 "2020-10-28T13:52:50Z")

</div>

Thanks for your reply. We were perhaps talking about different things (though I’m not _completely_ certain.)

> [@oheil](#):
>
> And now I learned that Matlab seems not to separate 1-dim Arrays (vectors) from 2-dim arrays (matrices), may be it is not needed for Matlab to distinguish because the implementation (probably in C) would not benefit in terms of memory usage and performance.

The problem with this in Matlab is really bad, not in terms of performance, but in terms of how it makes code a horrible mess. In my (very strong) opinion it was a very bad decision by Matlab.

But enough with the off-topic. Sorry.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 28, 2020, 1:53pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/12 "2020-10-28T13:53:38Z")

</div>

> [@Tamas\_Papp](#):
>
> I think you misunderstood my post: I am arguing that supporting the trailing `1` s makes little sense with how Julia works currently. It is a historical artifact, it did make some algorithms easier initially.

I see. But still it isn’t a bug and it is somehow _well_ documented.

> [@Tamas\_Papp](#):
>
> I would prefer catching `x::Vector[1,1]` etc as an error. But, again, that would break the API now.

Here I don’t have any opinion. Is there something planed for Julia 2.0?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 28, 2020, 1:54pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/13 "2020-10-28T13:54:48Z")

</div>

> [@DNF](#):
>
> But enough with the off-topic. Sorry.

No need for being sorry. I don’t think its off-topic, because everything may help OP to understand why something is as it is and how others think of it.

---

<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:** [October 28, 2020, 5:39pm UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/14 "2020-10-28T17:39:16Z")

</div>

> [@oheil](#):
>
> Is there something planed for Julia 2.0?

I don’t know where the threshold will be between revising minor warts (which not everyone considers warts, BTW) and keeping compatibility, but I suspect it will lean toward the latter. Cf

> [@PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872):
>
> You heard about this brand new exciting programming language called Julia and have some suggestions for how it should work. Wonderful! We’re happy to have you here and help you learn the language and listen to your ideas! However, please be aware that “new” is a relative term. Yes, Julia is newer than, say, C, which was first created in 1972 and standardized in 1989. It is not, however, new in the sense that it is still being freely designed and changed in arbitrary ways. Julia development start…

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [October 29, 2020, 3:32am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/15 "2020-10-29T03:32:05Z")

</div>

Ah. I did look at the docs before posting, but I missed that bit. Thanks for pointing it out.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [October 29, 2020, 3:57am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/16 "2020-10-29T03:57:46Z")

</div>

This was an interesting discussion - thank you everyone for responding.

Personally I’m with Tamas and DNF in that I don’t like the idea of allowing trailing ones when indexing arrays. To be clear, I’m _definitely_ not suggesting syntax changes at this stage of the language - I follow the discussions on this board enough to know that those types of posts don’t go down so well these days 🙂

I can perhaps add one point to the discussion which hasn’t been raised yet (and might be useful if this is ever discussed for v2.0). My logic for objecting to the syntax is that it can lead to accidental bugs when one mixes arrays of numbers with arrays of arrays of numbers in ones code. Personally, I often find myself in situations where I have `x::Matrix{Float64}` and `y::Vector{Vector{Float64}}`. Obviously for individual elements I index `x[n,m]` and `y[n][m]`. But sometimes I make a mistake and accidentally index `y[n,m]`.

From my point of view, life would be nice if this mistake errored, rather than me having to work backwards from another error in a different part of the code. And it is not hard to see how mixing higher dimensional arrays of numbers with arrays of arrays of arrays of numbers, or arrays of higher dimensional arrays of numbers, could lead to similar bugs that are harder to trace back (or worse, don’t result in errors at all). Of course, at heart, these types of bugs are my fault, not the languages, since the original indexing error is done by me. And to be fair, it is so wonderfully easy in Julia to define my own types that wrap these objects but allow me to choose the indexing rules. But as we all know when coding, some mistakes are easier to make than others, and this feels like a particularly easy one to make to me.

---

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [October 29, 2020, 6:11am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/17 "2020-10-29T06:11:59Z")

</div>

`Array{Int64, 2}` and `Array{Array{Int64, 1}, 1}` are different datatype.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [October 30, 2020, 2:08am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/18 "2020-10-30T02:08:43Z")

</div>

Yes understood. The discussion is about how the indexing rules (i.e. the `getindex` function) apply to these two types, and more generally, to higher-dimensional arrays of numbers vs arrays of arrays of numbers.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 31, 2020, 9:37am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/19 "2020-10-31T09:37:36Z")

</div>

Without breaking anything, perhaps there could be a macro that warns the developer of such thing. Like `@code_warntype`. Not related, but these days I used a filename that resulted to be forbidden in Windows. Someone used the term “linter” to suggest package that could track these small syntax issues and alert the programer.

There is one such linters already: [Messages - Lint.jl](https://lintjl.readthedocs.io/en/stable/messages/)

I am not sure if it is maintained. If so, that could be a pull request.

Edit: perhaps it supports that already: " \* More indices than dimensions in an array lookup"

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [November 2, 2020, 12:00am UTC](https://discourse.julialang.org/t/why-is-the-following-behaviour-not-a-bug/49169/20 "2020-11-02T00:00:52Z")

</div>

Nice find. It looks like the last PR was sometime in 2019 though…but yes, conceptually this is one solution.
