# Why throw on overlong slice?

**URL:** <https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345>\
**Category:** Internals & Design\
**Created:** [June 25, 2022, 11:56pm UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345 "2022-06-25T23:56:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [June 25, 2022, 11:56pm UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/1 "2022-06-25T23:56:54Z")

</div>

Python:

```julia
In [1]: [1,2][0:4]
Out[1]: [1, 2]

```

Julia:

```julia
julia> [1,2][1:5]
ERROR: BoundsError: attempt to access 2-element Vector{Int64} at index [1:5]

```

Why does Julia throw on overlong slice? Is this somehow useful for performance?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 26, 2022, 12:08am UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/2 "2022-06-26T00:08:39Z")

</div>

Yikes. Does that really work in python…?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [June 26, 2022, 12:26am UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/3 "2022-06-26T00:26:30Z")

</div>

It turns out to be nice to think of slicing as an intersection of indices. Under that interpretation, slicing outside the bounds of an array is a standard technique, rather than an error condition.

Python does it that way, as shown.

Ruby too

```julia
irb(main):002:0> [10,20][0,4]
=> [10, 20]

```

Haskell does it the same way:

```julia
Prelude> take 4 [1,2]
[1,2]

```

It’s also [recommended](https://youtu.be/bmSAYlu0NcY?t=2271) by Prof. John Ousterhout in his “A Philosophy of Software Design”.

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [June 26, 2022, 1:08am UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/4 "2022-06-26T01:08:57Z")

</div>

Atleast in my experience, this kind of thing leads to hard to find bugs in python, were something is initialized incorrectly but it fails silently instead of giving a bounds error.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [June 26, 2022, 6:28am UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/5 "2022-06-26T06:28:55Z")

</div>

> [@jar1](#):
>
> Haskell does it the same way:
> 
> ```julia
> Prelude> take 4 [1,2]
> [1,2]
> 
> ```

This is similar to julia first()/last() functions instead of slicing. And they don’t throw either: first([1, 2], 4) == [1, 2].

Actually slicing a vector in haskell is also restricted to valid indices, same as in julia: e.g. [Data.Vector](https://hackage.haskell.org/package/vector-0.7.0.1/docs/Data-Vector.html#v:slice).

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [June 26, 2022, 6:43am UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/6 "2022-06-26T06:43:56Z")

</div>

That’s a good point about `first`/`last`. There’s also

```julia
julia> [1,2][5:end]
Int64[]

```

though not

```julia
julia> [1,2][begin:5]
ERROR: BoundsError: attempt to access 2-element Vector{Int64} at index [1:5]

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 26, 2022, 7:09am UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/7 "2022-06-26T07:09:57Z")

</div>

> [@jar1](#):
>
> That’s a good point about `first`/`last`. There’s also
> 
> ```julia
> julia> [1,2][5:end]
> Int64[]
> 
> ```

That’s because `[1,2][5:end]` is equivalent to

```julia
a = [1,2]
a[5:lastindex(a)]

```

which makes an empty range:

```julia
julia> a = [1,2]
2-element Vector{Int64}:
 1
 2

julia> 5:lastindex(a)
5:4

julia> ans |> isempty
true

```

If it’s clear that the result would be empty, not having it error seems sensible to me. In contrast to `begin:5`, you’re never requesting data the array doesn’t have, since you’re not requesting any data at all.

---

<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:** [June 26, 2022, 11:29pm UTC](https://discourse.julialang.org/t/why-throw-on-overlong-slice/83345/8 "2022-06-26T23:29:22Z")

</div>

I’ll note that we’ve famously taken very different viewpoints on programming language design issues from Dr Ousterhout, ie. Ousterhout’s dichotomy vs the two language problem. Doesn’t mean he’s wrong about anything in particular but his recommendation may not necessarily hold positive weight.
