How do I get `[Element]` instead of `SubSequence` when dropping/removing elements?
I like to use this extension:
extension Sequence {
func asArray() -> [Element] {
return Array(self)
}
}
That said, both the Collection
and Sequence
protocols define a sequence()
method.
Sequence.prefix()
returns [Element]
while Collection.prefix()
returns a SubSequence
.
Given an Array
defined as follows, we can do some interesting things.
let myArray = [1, 2, 3, 4, 5]
The following works because the compiler knows to use the Sequence
version of suffix()
.
func lastFive() -> [Int] {
return myArray.suffix(5)
}
Here I'm just using the extension from above.
func lastFive2() -> [Int] {
return myArray.suffix(5).asArray()
}
The following also works because the compiler knows to use the Sequence
version of suffix()
.
func lastFive3() -> [Int] {
let suffix: [Int] = myArray.suffix(5)
return suffix
}
This does not compile because the compiler assumes you want to use the Collection
version of suffix()
.
func doesNotCompile() -> [Int] {
let suffix = myArray.suffix(5)
return suffix
}