Skip to content

Instantly share code, notes, and snippets.

@Ben-G
Last active February 25, 2016 01:07
Show Gist options
  • Save Ben-G/542cf4c526def3c51dbd to your computer and use it in GitHub Desktop.
Save Ben-G/542cf4c526def3c51dbd to your computer and use it in GitHub Desktop.
Type that forces a collection to not be empty
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