Suffix() returns Array but prefix() does not?

I am a bit puzzled and wonder if anybody here know why this is the case.

According to the documentation, Array functions suffix(Int) and prefix(Int) both returns ArraySlice. By accident I found out that suffix(Int) can actually return Array also.

Why is that?

struct Poc {
    let x: Int
    let y: Int
}

let x: [Poc] = [
    .init(x: 1, y: 1),
    .init(x: 2, y: 2),
    .init(x: 3, y: 3),
    .init(x: 4, y: 4),
]

// Compiles and runs fine
let a: [Poc] = x.suffix(2)

// Does not compile
let b: [Poc] = x.prefix(2)
1 Like

https://developer.apple.com/documentation/swift/sequence/suffix(_:)

This API produces an array rather than something like SuffixSequence<Self> because it has to traverse the sequence to find its end, at which point it might not be able to traverse it again.

4 Likes