Last active
June 29, 2016 04:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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) { | |
^ | |
*/ |
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
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.I think
associatedtype
has to evaluate to a type that isn't a protocol.