Skip to content

Instantly share code, notes, and snippets.

@JimRoepcke
Last active June 29, 2016 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimRoepcke/a584bf7df9bb5a55193d4ae1c09b008b to your computer and use it in GitHub Desktop.
Save JimRoepcke/a584bf7df9bb5a55193d4ae1c09b008b to your computer and use it in GitHub Desktop.
Please help me understand why the Swift 2.2 compiler in Xcode 7.3.1 says the class does not conform to the protocol
//: Playground - noun: a place where people can play
protocol Thingy: class {}
protocol ThingTaking: class {
associatedtype ThingType: Thingy
func some(thing: ThingType)
}
protocol MyThingy: Thingy {}
class WhyDoesThisNotConformToThingTaking: ThingTaking {
func some(thing: MyThingy) {
}
}
/*
Playground execution failed: Weird.playground:7:7: error: type 'WhyDoesThisNotConformToThingTaking' does not conform to protocol 'ThingTaking'
class WhyDoesThisNotConformToThingTaking: ThingTaking {
^
Weird.playground:6:20: note: unable to infer associated type 'ThingType' for protocol 'ThingTaking'
associatedtype ThingType: Thingy
^
Weird.playground:9:10: note: inferred type 'MyThingy' (by matching requirement 'some') is invalid: does not conform to 'Thingy'
func some(thing: MyThingy) {
^
*/
@CraigSiemens
Copy link

Not really a solution, more of an observation but if MyThingy is a class it works.

Also, if WhyDoesThisNotConformToThingTaking is setup to be a generic class it works.

class WhyDoesThisNotConformToThingTaking<T: Thingy>: ThingTaking {    
    func some(thing: T) {

    }
}

I think associatedtype has to evaluate to a type that isn't a protocol.

@zxhfirefox
Copy link

zxhfirefox commented Jun 29, 2016

I think, because compiler can not determined that when you actually call with some variable of WhyDoesThisNotConformToThingTaking like:
let x = WhyDoesThisNotConformToThingTaking (); x.some(y)
in above, y' s type can not be checked at compile time if some accept a protocol type.
when you add something like
class Thing : MyThingy {}
and change
func some(thing: MyThingy) { }
to
func some(thing: Thing) {}

it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment