Redundant superclass constraint

Original StackOverflow post:

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?

Hi Phil,

I think the warning is bogus in this case. Do you mind filing a bug?

Slava

···

On Oct 24, 2017, at 11:00 PM, Phil Kirby via swift-users <swift-users@swift.org> wrote:

Original StackOverflow post:

Redundant superclass constraint in swift 4 - Stack Overflow

<Redundant superclass constraint in swift 4 - Stack Overflow;
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?

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users