You’re right! The behavior should match what happens if there actually were elements at the specified offset. So the example should read:
"collection".insert("FOOBAR", at: .first - 7, padding: ".")
// ⟹ "FOOBAR.......collection"
"collection".insert("FOOBAR", at: .first - 5, padding: ".")
// ⟹ "FOOBAR.....collection"
"collection".insert("FOOBAR", at: .first - 3, padding: ".")
// ⟹ "FOOBAR...collection"
"collection".insert("FOOBAR", at: .first - 1, padding: ".")
// ⟹ "FOOBAR.collection"
Serves me right for posting with 2% brain capacity after a long day.
I don’t quite understand this statement. The whole point of this proposal is to introduce new collection behaviors and new collection APIs.
I fully agree with @Lantua here. The most exciting part of this proposal for me personally is that it gives us a sanctioned way to express locations that may be outside of the boundaries of a collection. This is brand new functionality and it is well within the scope of the proposal to define the best possible semantics for operations involving these new kind of locations. Offset bounds create a parallel universe to indices; we’re free to make up new rules on how they work.
(This is why I haven’t loudly complained yet about losing .end
— even though I feel extremely strongly that universal support for half-open bounds and zero-based counting is critically important for any stdlib feature that involves ranges and collections.)
The padding behavior for out-of-bounds insertions/replacements is nice in that unlike any other choice I’ve seen so far, the explicit padding
argument makes it obvious what the code is going to do when given a shorter than expected collection. (This also holds true for the fancy version that allows you to select the behavior you need.)