# Proposal: syntax for accessing arrays using relative indices

**URL:** <https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699>\
**Category:** Internals & Design\
**Created:** [January 23, 2020, 1:02pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699 "2020-01-23T13:02:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 23, 2020, 1:02pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/1 "2020-01-23T13:02:04Z")

</div>

Hi!

Let’s say I want to access 4 elements of an array. Today, I do something like:

```julia
data[1200:1203]
data[1200:1200+3]

```

or

```julia
data[i:i+3]

```

Wouldn’t be nice if we have a syntax to access elements using relative indices? Something like:

```julia
data[i+:4]

```

which will return 4 elements after (and including) `data[i]`? (This is not a good syntax, since it is currently valid and will return the element `data[i+4]`, I am just without ideas to propose a syntax for that, I just want to raise a discussion about the subject).

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 23, 2020, 1:14pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/2 "2020-01-23T13:14:02Z")

</div>

What’s wrong with `data[i.+(0:3)]`?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 23, 2020, 1:16pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/3 "2020-01-23T13:16:23Z")

</div>

I think the same problem with `[i:i+3]`, you “type” 3 but are accessing 4 elements.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 23, 2020, 1:18pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/4 "2020-01-23T13:18:57Z")

</div>

But that’s really always the problem with indexing. I personally would find `data[i+:4]` to mean `data[i:i+3]` more confusing.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 23, 2020, 1:21pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/5 "2020-01-23T13:21:01Z")

</div>

OK, I agree that indexing is problematic, leading to different opinions.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 23, 2020, 1:24pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/6 "2020-01-23T13:24:40Z")

</div>

We already have `range(i,length=4)`, which is clearer than an operator syntax.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 23, 2020, 1:27pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/7 "2020-01-23T13:27:23Z")

</div>

Ok, I can create a function for that to avoid too much typing. Thanks @stevengj!

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 23, 2020, 2:33pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/8 "2020-01-23T14:33:22Z")

</div>

There’s an issue (I don’t know where it is off the top of my head) and it suggested using a sort of filtering syntax for indexing.

```julia
X[>(5)]

```

Would get all indices greater than five. I’ve played around with it and really like it but I don’t think it would be a trivial thing to implement at this point.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 23, 2020, 2:44pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/9 "2020-01-23T14:44:44Z")

</div>

> [@Zach\_Christensen](#):
>
> I don’t think it would be a trivial thing to implement at this point.

```julia
Base.getindex(a::AbstractVector, f::Function) = filter(f, a)

```

works and is backwards-compatible since you can’t currently index arrays with functions.

(You’d also have to fix the interaction with `@view` and similar, e.g. by adding a method for `view(::AbstractVector, ::Function)`.)

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 23, 2020, 3:20pm UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/10 "2020-01-23T15:20:38Z")

</div>

I anticipate the difficulty would be dealing with more complex corner cases that result from multidimensional indexing and getting everyone to agree on implementation details. However, I’d be happy to be wrong about this and see it become a standard feature.

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 24, 2020, 12:12am UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/11 "2020-01-24T00:12:57Z")

</div>

Playing around for fun:

```julia
julia> struct Offset end

julia> off = Offset()
Offset()

julia> struct Off n end

julia> Base.:(*)(a, ::Offset) = Off(a)

julia> Base.:(:)(a, b::Off) = a:(a+b.n)

julia> 10:5off
10:15

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 24, 2020, 1:00am UTC](https://discourse.julialang.org/t/proposal-syntax-for-accessing-arrays-using-relative-indices/33699/12 "2020-01-24T01:00:35Z")

</div>

This kind of thing is precisely the reason why I just love this language 🙂
