Skip to content

Instantly share code, notes, and snippets.

@kouki-dan
Last active September 23, 2018 17:18
Show Gist options
  • Save kouki-dan/211c66ee327ca7905e61134578f4902f to your computer and use it in GitHub Desktop.
Save kouki-dan/211c66ee327ca7905e61134578f4902f to your computer and use it in GitHub Desktop.
複数の条件付きプロトコル適合
struct Hoge<T> {
let t: T
init(t: T) {
self.t = t
}
}
protocol FooProtocol {
}
class Foo: FooProtocol {}
protocol BarProtocol {
}
class Bar: BarProtocol {}
let hogeFoo1 = Hoge<FooProtocol>(t: Foo())
let hogeFoo2 = Hoge<FooProtocol>(t: Foo())
let hogeBar1 = Hoge<BarProtocol>(t: Bar())
let hogeBar2 = Hoge<BarProtocol>(t: Bar())
extension Hoge: Equatable where T == FooProtocol {
static func == (lhs: Hoge<T>, rhs: Hoge<T>) -> Bool {
return true // 本当は条件を書く
}
}
extension Hoge: Equatable where T == BarProtocol { // Error: Redundant conformance of 'Hoge<T>' to protocol 'Equatable'
static func == (lhs: Hoge<T>, rhs: Hoge<T>) -> Bool {
return true // 本当は条件を書く
}
}
print(hogeFoo1 == hogeFoo2)
print(hogeBar1 == hogeBar2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment