Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
final class Storage {
// not empty for testing
var keys: [String] = ["0"]
var values: [Int] = [0]
}
public struct Document {
var _storageReference: Storage
public init() {
self._storageReference = Storage()
}
public init(_ values: DocumentValues) {
self._storageReference = values._storageReference
}
public var values: DocumentValues {
get { return DocumentValues(self) }
set {
print("mutable view setter")
self = Document(newValue)
}
}
}
public struct DocumentValues : MutableCollection {
let _storageReference: Storage
init(_ document: Document) {
self._storageReference = document._storageReference
}
public var startIndex: Int {
return self._storageReference.keys.startIndex
}
public var endIndex: Int {
return self._storageReference.keys.endIndex
}
public func index(after i: Int) -> Int {
return self._storageReference.keys.index(after: i)
}
public subscript(position: Int) -> Int {
get { return _storageReference.values[position] }
set {
print("subscript setter")
self._storageReference.values[position] = newValue
}
}
}
// test
var document = Document()
let copy = document
document.values[0] = 1 // prints only (subscript setter)
/*
Problem:
- The view should clone the reference on the parrent and itself.
- The view should mutate in-place the value.
- Copy should remain with old values.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment