One reason I dislike base
properties in general is the semantic complexity they introduce to such simple abstractions as slices or trivial transformations.
Case in point: we evidently can't get base
to work consistently even in the most fundamental types in the stdlib.
var str = "Hello world"
let i = str.firstIndex(of: " ")!
var s1 = str[i ..< i]
s1.replaceSubrange(s1.endIndex ..< s1.endIndex, with: ", cruel")
print(s1.base) // "Hello, cruel world"
var s2 = str[i ..< i]
s2.append(contentsOf: ", cruel")
print(s2.base) // ", cruel"
(Of course, this inconsistency also surfaces through indices, somewhat damaging the collection abstraction. But at least append
is implicitly* documented to invalidate indices. (Not that other parts of the stdlib care much about such minor details as continuing to use invalid indices...))
* (The passage that spells this out is actually missing from the append
docs. Yay, yet another bug!)
Substring.base
is ill-defined. To make base
a general expectation, at minimum I'd like us to provide a specification of how it is supposed to interact with mutations. (Or the expectation strictly limited to @frozen
& read-only collection transformations.)