Created
September 12, 2017 14:21
-
-
Save allevato/e1aab2b7b2ced72431c3cf4de71d306d to your computer and use it in GitHub Desktop.
Wrapper for transient properties to use with synthesized protocol defaults
This file contains hidden or 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
/// Wraps a value but causes it to be treated as "value-less" for the purposes | |
/// of automatic Equatable, Hashable, and Codable synthesis. This allows one to | |
/// declare a "cache"-like property in a value type without giving up the rest | |
/// of the benefits of synthesis. | |
public enum Transient<Wrapped>: Equatable, Hashable, Codable { | |
case none | |
case some(Wrapped) | |
public static func == (lhs: Transient<Wrapped>, rhs: Transient<Wrapped>) -> Bool { | |
// By always returning true, transient values never produce false negatives | |
// that cause two otherwise equal values to become unequal. In other words, | |
// they are ignored for the purposes of equality. | |
return true | |
} | |
public var hashValue: Int { | |
// Transient values do not contribute to the hash value. (This could be any | |
// constant.) | |
return 0 | |
} | |
public init(from decoder: Decoder) { | |
// Since the value is transient, it's not encoded, so at decode-time we | |
// merely clear it. | |
self = .none | |
} | |
public func encode(to encoder: Encoder) throws { | |
// Transient properties do not get encoded. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment