4 ms·
For me it's exactly the other way around. Having "first" as a property feels like exposing implementation details to me. Sure, a array/vector is a contiguous s
by the_duke 8y ago
For me it's exactly the other way around.
Having "first" as a property feels like exposing implementation details to me. Sure, a array/vector is a contiguous slice of memory filled with items of a certain size. But the vector/array is is basically a pointer to the start of the memory + a size, so "first" is not a inherent part of the data structure, rather a computed/derived property.
When I see a property I think member of a struct, which is not the case here.
(ps: this is all really just hair splitting, it's a minor difference that you would get used to pretty quickly either way)
- wool_gather 8y agoIt's a reasonable point. But here `first` is actually declared in the `Collection` interface, which `Array` implements. There's no guarantee about what it's actually doing. (Although possibly it has a documented expectation to be O(1), can't remember at the moment.)
- saagarjha 8y agoCollection's startIndex and subscripting is supposed to be O(1), so I'd assume that first is also supposed to be O(1) since it's trivial to implement it by composing the first two (in fact, this is how Swift implements it by default if you don't override it: https://github.com/apple/swift/blob/e08b2194487d883896a377a0f343577775845cd3/stdlib/public/core/Collection.swift#L1095 https://github.com/apple/swift/blob/e08b2194487d883896a377a0...)