# OffsetArrays, inbounds, and confusion

**URL:** <https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295>\
**Category:** Internals & Design\
**Tags:** indexing, arrays\
**Created:** [May 19, 2022, 3:50am UTC](https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295 "2022-05-19T03:50:46Z")\
**Posts on this page:** 1\
**Showing post:** 49

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [May 20, 2022, 4:46pm UTC](https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295/49 "2022-05-20T16:46:06Z")

</div>

~~For generic code I’d refrain from using `[begin+4:end-4]`,  
as it would fail on “strided” arrays (e.g. arrays stored in compact form, but with only even indices).  
~~

_Edit: leaving the part below, which shows how indexing on `eachindex` surprised me._

I read Paulo’s post as “iterate from the 5th element to the 4th before last”,  
which seems a valid use case.

To do that, one can’t index on the `eachindex(oa)` iterator itself

```julia
oa = OffsetArray(["a", "b", "c", "d", "e"], -2:2)
eachindex(oa)[3:5]
BoundsError: attempt to access 5-element OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}} with indices -2:2 at index [3:5]

```

I currently do not understand what it is trying to achieve.

But on the collected indexes, it works nicely:

```julia
oa = OffsetArray(["a", "b", "c", "d", "e"], -2:2)

# loop on the 3rd to 5th element
for idx in collect(eachindex(oa))[3:5]
	println("$idx: $(oa[idx])")
end

0: c
1: d
2: e

```

I’d expect `Base.iterators.take(iterator, 3:5)` to return a nice iterator for this purpose,  
but it’s not the case yet (as of julia-1.6.5).

Julia is already awesome, and progressing quickly in the right direction. Keep on the good work !

---

_[View the full topic](https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295)._
