Skip to content

Instantly share code, notes, and snippets.

@DevAndArtist
Created November 18, 2016 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevAndArtist/9347c40702f99abd4f31e5539d1819df to your computer and use it in GitHub Desktop.
Save DevAndArtist/9347c40702f99abd4f31e5539d1819df to your computer and use it in GitHub Desktop.
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