[swift-users] Redundant superclass constraint

Phil Kirby phil.kirby at gmail.com
Wed Oct 25 00:58:25 CDT 2017


Original StackOverflow post:

https://stackoverflow.com/questions/46924554/redundant-superclass-constraint-in-swift-4

I'm getting a Redundant superclass constraint... warning in swift4: (paste in a playground)
import CoreData

class Item : NSManagedObject {}

protocol DataSourceProtocol {
   associatedtype DataSourceItem : NSManagedObject
}

protocol DataSourceProtocolProvider : class { }

extension DataSourceProtocolProvider {
   func createDataSource<T: DataSourceProtocol>(dataSource: T)
        where T.DataSourceItem == Item {
   }
}
On the createDataSource<T: DataSourceProtocol> declaration I get the following warning:
Redundant superclass constraint 'T.DataSourceItem' : 'NSManagedObject'
I thought that you could specify that an associatedtype could be used with the == operator to constrain the associatedtype to a specific type. I want to have a func createDataSource<T: DataSourceProtocol>(dataSource:T) where the DataSourceItem is an Item.
If I replace the == operator with : then the warning goes away:
extension DataSourceProtocolProvider {
   func createDataSource<T: DataSourceProtocol>(dataSource: T)
        where T.DataSourceItem : Item {
   }
}
This happens to be a completely different context now. This constraint specifies that I want to have a func createDataSource<T: DataSourceProtocol>(dataSource:T) where the DataSourceItem is a subclass of Item. Which isn't the same thing as DataSourceItem is an Item object. Also, the code runs fine with == so am I just not understanding how constraints work?

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


More information about the swift-users mailing list