# Unexpected (to me) Array behavior

**URL:** <https://forums.swift.org/t/unexpected-to-me-array-behavior/36243>\
**Category:** Using Swift\
**Tags:** array\
**Created:** [May 7, 2020, 4:28am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243 "2020-05-07T04:28:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Eadie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gavin_eadie/32/40830_2.png) [@Gavin\_Eadie](https://forums.swift.org/u/Gavin_Eadie)\
**Post date:** [May 7, 2020, 4:28am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/1 "2020-05-07T04:28:51Z")

</div>

I wasn't expecting the "Index out of bounds" error on `combo2` .. maybe I should have been. Apparently `combo2` is 'inheriting' the indices of `array1` and since indices 0 and 1 of `array1` were not included in `combo2`, they don't exist there. Explicitly typing `combo1` as `[Int]` gives me the behavior I expected (at, I suspect, the cost of copying the array).

```
let array1 = [0,1,2,3,4]
let array2 = [9,8,7,6,5]

let combo1: [Int] = array1[2...] + array2
let combo2 = array1[2...] + array2

var wowee1 = combo1[3]
wowee1 = combo1[2] // = 4
wowee1 = combo1[1]
wowee1 = combo1[0]

var wowee2 = combo2[3]
wowee2 = combo2[2] // = 2
wowee2 = combo2[1] // Fatal error: Index out of bounds: file
                        // /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/
                        // swiftlang/swiftlang-1103.8.25.8/swift/stdlib/public/core/SliceBuffer.swift, line 290

```

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [May 7, 2020, 4:37am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/2 "2020-05-07T04:37:06Z")

</div>

So `combo2` isn't an Array, it's an ArraySlice, and those aren't always 0 based for their indexes. In this case `combo2`'s first index is 2, so 0 is out of bounds.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [May 7, 2020, 4:38am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/3 "2020-05-07T04:38:24Z")

</div>

> [@Gavin\_Eadie](#):
>
> Apparently `combo2` is 'inheriting' the indices of `array1`

This is correct. Expressions like `array1[2...]` create `ArraySlice`s, not `Array`s. You can read more about this behavior [here](https://developer.apple.com/documentation/swift/arrayslice) (see **Slices Maintain Indices**.

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [May 7, 2020, 5:01am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/4 "2020-05-07T05:01:56Z")

</div>

The other replies give the correct answer, but I just want to add one point. (Array) slices maintaining the same indexes they were created with is a _feature_. This way, an index that is valid in the slice not only is valid in the original, but _it refers to the same element_.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [May 7, 2020, 5:04am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/5 "2020-05-07T05:04:51Z")

</div>

The others’ answers are correct. If you want to know the reasoning for it, or why `: [Int]` fixes it, see here:

> [@Data.popFirst(), removeFirst() adjust indices](https://forums.swift.org/t/data-popfirst-removefirst-adjust-indices/35965/5):
>
> A fundamental requirement of the Collection protocol is that [slices share indices](https://developer.apple.com/documentation/swift/collection#2889677) with the parent collection. It is incredibly useful, because it means that for any valid index, slice[index] == collection[index], and many generic collection algorithms are much more efficient because of it. When you want to reset the indices for some reason, just create a fresh Data instance from the Data.SubSequence: let slice = myData.dropFrist(20) let reset = Data(slice) assert(reset.startIndex == 0) // ✓ B…

(That post is about `Data`, but it applies to `Array` too.)

* * *

> ```swift
> let combo2 = array1[2...] + array2
> 
> ```

The fact that this even compiles seems like an unfortunate accident to me. It is very unclear what type it is supposed to produce. I’ve been working with Swift for years and even I had to run it through the compiler to find out that it results in an `ArraySlice`. (I expected some sort of `FlattenSequence`.) But this seems to be a butchering of what a slice is supposed to be. A slice of _what_? Printing it demonstrates that all the right elements are there. But the indices correspond neither to 0‐based logic, nor to the indices of any of its components. And slices hold onto the entire memory of their base collection. What memory is that abomination holding onto behind the scenes? Or allocating?

Even though `+` is available (presumably it was necessary for protocol conformance), I don’t recommend ever using it on an `ArraySlice`. Do something like the following instead, so that you always know very clearly what is happening with indices and memory under the hood:

```swift
let combo2 = Array(array1[2...]) + array2

```

---

<div class="post-metadata">

**Author:** ![Gavin\_Eadie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gavin_eadie/32/40830_2.png) [@Gavin\_Eadie](https://forums.swift.org/u/Gavin_Eadie)\
**Post date:** [May 7, 2020, 5:16am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/6 "2020-05-07T05:16:13Z")

</div>

> "incredibly useful"

I live and learn .. many thanks.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [May 7, 2020, 5:38am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/7 "2020-05-07T05:38:46Z")

</div>

Really, it's a feature? Because my expectation is that an expression like "array1[2...] + array2" is simply a collection of elements as if I had written, array1[2] + array1[3],,array1[n] + array2[0],,array2[n]. I don't expect metadata to come with it, I want a list of values. In my mind, this is confusion waiting to happen.

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [May 7, 2020, 5:46am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/8 "2020-05-07T05:46:55Z")

</div>

It _is_ a collection of elements. The confusion comes because you think Collections are indexed from zero: that is, that `collection[0]` is the first element. In Swift, this is not true. The valid range of indices for a collection runs from `startIndex` to `endIndex`.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 7, 2020, 5:49am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/9 "2020-05-07T05:49:57Z")

</div>

In fact, the only `Collection` that is guaranteed to have `0`-as-start `1`-as-step is `Array`. `Data` does come close, but falls short as it is its own subsequence.

Edit:

Now that I checked the `Array` doc, it actually doesn't say anything about the nature of `startIndex` and its step function 🧐.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [May 7, 2020, 5:52am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/10 "2020-05-07T05:52:58Z")

</div>

In my mind, that's a calamity. The idiom of getting to the first element of an array with arrayname[0] is a 35 year old habit.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [May 7, 2020, 5:55am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/11 "2020-05-07T05:55:48Z")

</div>

Wait, what? All we are talking about here is arrays. The examples are array literals and simple declarations like [Int].

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 7, 2020, 5:57am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/12 "2020-05-07T05:57:32Z")

</div>

> [@Hacksaw](#):
>
> The idiom of getting to the first element of an array with arrayname[0] is a 35 year old habit.

It becomes a 35-year-old implementation burden though, as all kinds of collection needs to figure out how to make it works with that paradigm.

> [@Hacksaw](#):
>
> All we are talking about here is arrays.

`ArraySlice` 😉.

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [May 7, 2020, 5:58am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/13 "2020-05-07T05:58:25Z")

</div>

And it works fine. The above code isn’t getting the first element of an Array, it’s getting the first element of an ArraySlice. Different thing. If you want an Array, you can wrap whatever you have in `Array()`.

With that said, I should clarify that I totally understand where you’re coming from. Lots of users have a trajectory into Swift where they enter the language, pick up `Array` as the first collection they use, and instantly subscript it. This appears to work, so they are not forced to understand Swift’s `Collection` model.

In many ways, the real mistake was to make `Array`‘s `Index` `Int`. This was done because Swift had no notion of offset-based indexing (and indeed still does not, though it’s in progress: see [Offset Indexing and Slicing](https://forums.swift.org/t/offset-indexing-and-slicing/28333)). If `Array`’s `Index` were opaque, it would force users to confront their understanding of what the system actually does.

And the system is defined sensibly. `Index` is not intended to be a convenient type for the user, it’s intended to be a convenient type _for the `Collection`_. The goal of `Collection.Index` is, roughly, if you have one you should be able to subscript `Collection` with it and get the element in `O(1)`. For `Array`, an `Int` is fine. But for other `Collection`s we use other types. `Dictionary` uses an opaque type that ends up holding an offset into the `Collection`. NIO’s `CircularBuffer` does a similar thing. You can construct weird `Collection`s over things like the `getaddrinfo` list by making the `Index` type a pointer.

This solution is very flexible, but without offset-based indexing it bumps into the confusion boundary for many users very quickly. The Swift community really ought to push offset-based indexing over the line to help avoid these problems.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [May 7, 2020, 6:00am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/14 "2020-05-07T06:00:28Z")

</div>

If an ArraySlice is returned from that expression, that's a magic expression. It breaks the principle of least astonishment.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 7, 2020, 6:04am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/15 "2020-05-07T06:04:32Z")

</div>

> [@Hacksaw](#):
>
> It breaks the principle of least astonishment

Ehh, that'd be a hard call. Given that Swift subscript rarely returns the same type. Admittedly it can look deceiving coming from other languages, but that's actually the most consistent as part of the language.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [May 7, 2020, 6:06am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/16 "2020-05-07T06:06:41Z")

</div>

I'd agree with the idea that if there is some amazing feature you get from having an opaque index, great. But don't call it array, and not expect people to be pissed off if it doesn't look like every other array they've dealt with.

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [May 7, 2020, 6:07am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/17 "2020-05-07T06:07:59Z")

</div>

I mean, again, Array behaves the way you want it to. ArraySlice does not.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [May 7, 2020, 6:10am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/18 "2020-05-07T06:10:00Z")

</div>

I don't think it's too hard a call, I am pretty damned astonished.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 7, 2020, 6:10am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/19 "2020-05-07T06:10:48Z")

</div>

> [@Hacksaw](#):
>
> I am pretty damned astonished

Sure.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [May 7, 2020, 6:15am UTC](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243/20 "2020-05-07T06:15:52Z")

</div>

I understand that, my problem is that I never would have expected what looks like a basic syntax to return a new type, and especially not a new type with a strange behavior. I can't think of another language which does this, why would anyone expect it?

[Next page](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243.md?page=2)
