Created
November 18, 2016 17:27
-
-
Save DevAndArtist/9347c40702f99abd4f31e5539d1819df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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