[swift-users] Generics with Protocols - Separate files

somu subscribe somu.subscribe at gmail.com
Sat Sep 16 21:55:31 CDT 2017


Hi,

When using Generics with Protocols I get a compilation error depending on whether or not conformance to a protocol is defined in the same file or not.

Error: 
Type 'Car.ItemID' (aka 'CarID') does not conform to protocol 'P2'

I have 2 swift files:
- CarID.swift
- CreateCars.swift

Observations:
- I get the error when extension CarID : P2 {} is defined in CarID.swift
- I don’t get the error when extension CarID : P2 {} is defined in CreateCars.swift as shown in the comments below.

Tested On:
- Xcode 9.0 (9A235) - GM
- Command Line project / iOS project


CarID.swift:

import Foundation

protocol P1   {
    associatedtype ItemID
}

class Car : P1 {
    typealias ItemID = CarID
}

//extension CarID : P2 {} //Error wouldn't occur if this line is uncommented and the line in CarID.swift is commented out

protocol P2 {}

class CreateItems<Something : P1>
where Something.ItemID : P2 {}

class CreateCars : CreateItems<Car> {} //Error: Type 'Car.ItemID' (aka 'CarID') does not conform to protocol 'P2'

extension CreateCars {}



CreateCars.swift:

import Foundation

class CarID {}

extension CarID : P2 {} //If this line is moved to CreateCars.swift the error vanishes away


Thanks and regards,
Muthu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170917/86379cad/attachment.html>


More information about the swift-users mailing list