There seems to be no formal definition of what Apple means by destructive behavior when referring to the expectations that developers should have when calling a for in
loop on a sequence more than one time. While the example alluded to in the documentation refers to a Countdown
sequence which counts down from 3 to 0, there is an additional case which is not considered where Countdown
is initialised with a negative value. I have researched the meaning of destructive behaviour of a Sequence and came across an article which decrements the value of a startId
property of a sequence using an iterator’s next()
method which is marked as mutating
. When a while let
statement is used to iterate over the sequence over a second pass, the statement does not return any property. This seems to imply that the original break condition has been met when calling the while let
the first time and that the final startId
value has been returned which means that the sequence has ended. That article can be found here. Moreover, there is a discussion on the Swift.org site which talks about the idea of separating concepts like “finiteness” from “destructibility” but doesn’t really go into much detail to describe what “destructive” means. Can someone please explain this?
1
“Destructive” isn’t a formally defined term, but in this instance, it means that some sequences produce iterators that modify the sequence itself, meaning that you can “use up” the elements from one iteration, and make them unavailable to any future iterations.
Consider this OneShotSequence
class:
class OneShotSequence {
private var current: Int = 0
private let end: Int
init(end: Int) { self.end = end }
}
extension OneShotSequence: IteratorProtocol {
func next() -> Int? {
guard current < end else { return nil }
defer { current += 1 }
return current
}
}
extension OneShotSequence: Sequence {
func makeIterator() -> OneShotSequence {
// An OSS object is its own iterator,
// meaning that iteration mutates the original OSS object.
// This only happens if OSS is a class, because if it were a struct,
// this return value would be copied, and not have this issue.
return self
}
}
It behaves really similarly to a range:
print(Array(0...<5)) // => [0, 1, 2, 3, 4]
print(Array(OneShotSequence(end: 5))) // => [0, 1, 2, 3, 4]
But there’s one huge distinction: an OneShotSequence
object is is own iterator. This means that as you call next()
on the iterator, it modifies the actual sequence object, consuming its elements:
var sequence = OneShotSequence(end: 5)
// First iteration
for value in sequence {
print(value) // prints 0, 1, 2, 3, 4
}
// Second iteration doesn't print anything, since it has already been "spent"
for value in sequence {
print(value) // never called
}
These kinds of sequences are rare, but do crop up from time to time. Consider a sequence that models the messages coming in from a message queue. They might be buffered (so that new messages can be received while you’re still working through previous ones). Once you consume a message with next()
, if you don’t save it, it’s gone. Of course, Sequence
could have been implemented in a way that automatically retains all the previous elements indefinitely, but that would be a memory hog in cases where it isn’t necessary.
3