Created
January 26, 2019 03:09
-
-
Save davertay/47ae412a7dd5b814414439bae13ccbad to your computer and use it in GitHub Desktop.
An Experiment With Changeable Writers
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
// An experiment with isolating the key path writing mechanism | |
public struct Changeable<T> { | |
public let value: T | |
public let hasChanged: Bool | |
public static func write<U: Equatable>(_ keyPath: WritableKeyPath<T, U>, _ value: U) -> (inout T) -> Changeable<T> { | |
return { whole in | |
let changed = whole[keyPath: keyPath] != value | |
if changed { | |
whole[keyPath: keyPath] = value | |
} | |
return Changeable(value: whole, hasChanged: changed) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment