Created
December 11, 2015 21:08
-
-
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
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
//: 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