4 ms·
This is because Swift is quite low-level when it comes to storage etc., so [0..<n] returns an ArraySlice. However, this has nothing to do with generics.If you'
by Tainnor 7y ago
This is because Swift is quite low-level when it comes to storage etc., so [0..<n] returns an ArraySlice.
However, this has nothing to do with generics.If you're willing to sacrifice performance, you could easily write
extension Array {
func take(_ n: Int) -> [Element] {
Array(self[0..<n])
}
}