Generic function does not work as expected

Hello, Please look at the code below. When compiling, I am getting an error "Global function 'fetchingFunc(entityType:)' requires that 'NSManagedObject' conform to 'CloudKitManagedObject'". Somehow the row 22 works great, but row 27 doesn't work, though everything looks logically.
Row 27 will work if I will make fetchingFunc(entityType:) not generic receiving CloudKitManagedObject directly. But in that case NSFetchRequest<CloudKitManagedObject> at row 18 doesn't work.

For me not working row 27 looks like some sort of a bug, don't see any reason it should not work.

Actually the goal here is to make row 18 working, could not achieve that without making function generic.

Code Block Swift
import CloudKit
import CoreData
protocol CloudKitManagedObject where Self: NSManagedObject {
    static var entityName: String { get }
}
extension CloudKitManagedObject where Self: NSManagedObject {
    static func fetchRequest() -> NSFetchRequest<Self> {
        return NSFetchRequest<Self>(entityName: self.entityName)
    }
}
class Item: NSManagedObject, CloudKitManagedObject { static let entityName = "Item" }
class List: NSManagedObject, CloudKitManagedObject { static let entityName = "List" }
func fetchingFunc<SyncObj: CloudKitManagedObject>(entityType: SyncObj.Type) {
    let fetchRequest: NSFetchRequest<SyncObj> = entityType.fetchRequest()
    fetchRequest.predicate = nil
}
fetchingFunc(entityType: Item.self)
let types: [CloudKitManagedObject.Type] = [Item.self, List.self]
for type in types {
    fetchingFunc(entityType: type) //This won't compile
}



For me not working row 27 looks like some sort of a bug

Unfortunately, it is not a bug. In Swift, parameter types of generics needs to be resolved in compile-time.
In your case, the parameter type SyncObj of the generic func fetchingFunc needs to be resolved statically.

If you want to keep your fetchingFunc generic, you may need to give up using it dynamically like in line 27.
Generic function does not work as expected
 
 
Q