Skip to content

Instantly share code, notes, and snippets.

@davertay
Created January 26, 2019 03:09
Show Gist options
  • Save davertay/47ae412a7dd5b814414439bae13ccbad to your computer and use it in GitHub Desktop.
Save davertay/47ae412a7dd5b814414439bae13ccbad to your computer and use it in GitHub Desktop.
An Experiment With Changeable Writers
// 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