Skip to content

Instantly share code, notes, and snippets.

@NachoSoto
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NachoSoto/1f0e7ed17ec6cd50607c to your computer and use it in GitHub Desktop.
Save NachoSoto/1f0e7ed17ec6cd50607c to your computer and use it in GitHub Desktop.
Generic parameter ’T’ cannot be bound to non-@objc protocol type ‘Protocol’.
protocol BaseProtocol {}
protocol ExtendedProtocol: BaseProtocol {}
class A<T: BaseProtocol> {
private let t: T
init(a: T) { self.t = a }
}
class B<T: ExtendedProtocol> {
private let a: A<T>
init(a: A<T>) { self.a = a }
}
class ExtendedClass: ExtendedProtocol {}
// This isn't possible either :(
let a: A<ExtendedProtocol> = A(a: ExtendedClass())
let b: B<ExtendedProtocol> = B(a: a)
protocol Protocol {}
class A<T: Protocol> {
private let array: [T]
init(a: [T]) { self.array = a }
}
class Class1: Protocol {}
class Class2: Protocol {}
let array: [Protocol] = [
Class1(),
Class2()
]
// Generic parameter ’T’ cannot be bound to non-@objc protocol type ‘Protocol’.
let a: A<Protocol> = A(a: array)
// Unfortunately `T` can only be a class type, not a protocol.
// But `Class1` and `Class2` are only related by a protocol.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment