[swift-users] Type "T?" does not conform to protocol 'Equatable'

Rudolf Adamkovič salutis at me.com
Mon Feb 29 09:36:21 CST 2016


Got it. Thanks!

R+

> On 29 Feb 2016, at 16:07, Keith Duvall via swift-users <swift-users at swift.org> wrote:
> 
> Don’t mark it as optional in the generic definition. Mark it as optional in your implementation.
> 
> class C<T: Equatable> {
> 	let t: T?
> 	init(t: T?) {
> 		self.t = t
> 	}
> }
> 
> let a = C<Int>(t: nil)
> 
> print(a.t) // nil
> 
> let b = C<Int>(t: 5)
> 
> print(b.t) // Optional(5)
> 
> Keith
> 
> 
>> On Feb 29, 2016, at 9:24 AM, Rudolf Adamkovič via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> Hi everyone!
>> 
>> I have a generic class similar to this one:
>> 
>> class C<T: Equatable> {
>>     let t: T
>>     init(t: T) { self.t = t }
>> }
>> 
>> When try to wrap ‘Int?' inside, I get the following error:
>> 
>> let a = C<Int?>(t: nil) // ERROR: Type "Int?" does not conform to protocol 'Equatable'
>> 
>> Yet when I try to compare two ‘Int?’ values, everything works:
>> 
>> let a: Int? = 5
>> let b: Int? = 6
>> 
>> let c = a == b // NO ERROR
>> 
>> So, is ‘Int?' equatable or not?
>> 
>> Thanks!
>> 
>> R+
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160229/f9801582/attachment.html>


More information about the swift-users mailing list