Skip to content

Instantly share code, notes, and snippets.

@cfilipov
Created December 11, 2015 21:08
Show Gist options
  • Save cfilipov/c9929dcb2cffcbf52a22 to your computer and use it in GitHub Desktop.
Save cfilipov/c9929dcb2cffcbf52a22 to your computer and use it in GitHub Desktop.
Swift 2.0 Protocols & Generic Constraints in response to http://inessential.com/2015/07/19/secret_projects_diary_2_swift_2_0_prot
//: Playground - noun: a place where people can play
import Cocoa
protocol Feed: Equatable {
var url: String {get}
}
func ==<F: Feed>(lhs: F, rhs: F) -> Bool {
return lhs.url == rhs.url
}
protocol Folder {
typealias FeedType
var feeds: [FeedType] {get}
func addFeeds(feedsToAdd: [FeedType])
}
class LocalFeed: Feed {
var url: String
init(url: String) {
self.url = url
}
}
class LocalFolder<F: Feed>: Folder {
var feeds = [F]()
func addFeeds(feedsToAdd: [F]) {
for oneFeed in feedsToAdd {
if !feeds.contains(oneFeed) {
feeds += [oneFeed]
}
}
}
}
let localFeed = LocalFeed(url: "example.com")
let folder = LocalFolder<LocalFeed>()
folder.addFeeds([localFeed])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment