Last active
August 29, 2015 14:15
-
-
Save insanehunter/fe199aabb1334ec2e5ab to your computer and use it in GitHub Desktop.
Versioned: monad-like structure for holding version number along with any value
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
public struct Versioned<T> { | |
public init(_ value: T) { | |
self.value = value | |
self.version = VersionGenerator.generateVersion() | |
} | |
public init(_ value: T, version: VersionGenerator.VersionType) { | |
self.value = value | |
self.version = version | |
} | |
public let value: T | |
public let version: VersionGenerator.VersionType | |
} | |
public func ==<T: Equatable>(a: Versioned<T>, b: Versioned<T>) -> Bool { | |
return a.version == b.version && a == b | |
} | |
public struct VersionGenerator { | |
public typealias VersionType = UInt64 | |
private static func generateVersion() -> VersionType { | |
return ++VersionGenerator.currentVersion | |
} | |
private static var currentVersion: VersionType = 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: