Skip to content

Instantly share code, notes, and snippets.

@brentsimmons
Created August 5, 2015 18:07
Show Gist options
  • Save brentsimmons/7ec7e3669ff7ad17e446 to your computer and use it in GitHub Desktop.
Save brentsimmons/7ec7e3669ff7ad17e446 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Cocoa
protocol Account: Equatable {
var accountID: String {get}
}
// https://twitter.com/optshiftk/status/628985834801336320
func ==<T: Account>(lhs: T, rhs: T) -> Bool {
return lhs.accountID == rhs.accountID
}
class FooAccount: Account {
let accountID = "foo"
}
class BarAccount: Account {
let accountID = "bar"
}
let foo = FooAccount()
let bar = BarAccount()
var accounts = [Account]()
// Error on line 29: protocol 'Account' can only be used as a generic constraint because it has Self or associated type requirements
@wildthink
Copy link

How about this?

import Cocoa

protocol Feed {
    var url: String {get}
    func isEqualTo(other: Feed) -> Bool
}

extension Feed where Self: Equatable {
    func isEqualTo(other: Feed) -> Bool {
        return url == other.url
    }
}

func ==<T: Feed where T:Equatable> (lhs: T, rhs: T) -> Bool {
    return lhs.isEqualTo(rhs)
}

protocol Folder {
    var feeds: [Feed] {get}
    func addFeeds(feedsToAdd: [Feed])
}

// This could be done if the Folder feeds had a { get set }
//extension Folder {
//    func addFeeds(feedsToAdd: [Feed]) {
//        for oneFeed in feedsToAdd {
//            if !feeds.contains({ $0.isEqualTo (oneFeed) }) {
//                feeds += [oneFeed]
//            }
//        }
//    }
//}

class LocalFeed: Feed {
    var url: String
    init(url: String) {
        self.url = url
    }
}
extension LocalFeed: Equatable {}

class LocalFolder: Folder {

    var feeds = [Feed]()

    func addFeeds(feedsToAdd: [Feed]) {
        for oneFeed in feedsToAdd {
            if !feeds.contains({ $0.isEqualTo (oneFeed) }) {
                feeds += [oneFeed]
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment