[swift-users] Generics with Protocols - Separate files

somu subscribe somu.subscribe at gmail.com
Sun Sep 17 06:44:48 CDT 2017


Sorry about the typo (filenames for the code were mentioned wrong).

The corrected filenames are as follows (problem still occurs):

Question:
- Why does the error occur when extension CarID : P2 {} is defined in CarID.swift ?


CreateCars.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 {}



CarID.swift:

import Foundation

class CarID {}

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


Thanks and regards,
Muthu





> On 17 Sep 2017, at 10:55 AM, somu subscribe via swift-users <swift-users at swift.org> wrote:
> 
> 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
> _______________________________________________
> 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/20170917/0435512b/attachment.html>


More information about the swift-users mailing list