Last active
February 25, 2016 01:07
-
-
Save Ben-G/542cf4c526def3c51dbd to your computer and use it in GitHub Desktop.
Type that forces a collection to not be empty
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 NonEmptyCollection<T: CollectionType> { | |
let collection: T | |
init?(collection: T) { | |
if !collection.isEmpty { | |
self.collection = collection | |
} else { | |
return nil | |
} | |
} | |
} | |
public extension NonEmptyCollection where T: ArrayLiteralConvertible { | |
init(element: T.Element) { | |
self.collection = T(arrayLiteral: element) | |
} | |
} | |
/* | |
Example Usage in API: | |
func receiveUpdates(NonEmptyCollection<[Update]>) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment