# 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:** 1\
**Showing post:** 5

<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

```

---

_[View the full topic](https://forums.swift.org/t/unexpected-to-me-array-behavior/36243)._
