Conformed Failable<MutableCollection, T> type to MutableCollection protocol

This commit is contained in:
Caleb Kleveter 2018-12-03 06:57:12 -06:00
parent b2f62e5d5f
commit 91c547c21a
No known key found for this signature in database
GPG Key ID: B38DBD5CF2C98D69
1 changed files with 21 additions and 0 deletions

View File

@ -36,3 +36,24 @@ extension Failable: BidirectionalCollection where T: BidirectionalCollection {
} }
extension Failable: RandomAccessCollection where T: RandomAccessCollection {} extension Failable: RandomAccessCollection where T: RandomAccessCollection {}
extension Failable: MutableCollection where T: MutableCollection {
/// See [`MutableCollection.[]`](https://developer.apple.com/documentation/swift/mutablecollection/1640969-subscript).
///
/// To validate the new array, the instance is copied and mutated, then the validation is run on the mutated instance.
/// If the validation succeeds, the mutated value is assigned to the current instance. If the validation fails, the set silently fails.
public subscript(position: T.Index) -> T.Element {
get {
return self.value[position]
}
set(newValue) {
var copy = self
copy.value[position] = newValue
do {
try self <~ copy.value
} catch {}
}
}
}