Last active
August 24, 2016 21:12
-
-
Save nvh/576485900719b9b5046ba9027130390c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
class Animal: NSObject { | |
} | |
protocol Named { | |
init(name: String) | |
} | |
protocol AnimalSpecification { | |
associatedtype AnimalType: Animal | |
} | |
//This workaround doesn't add any extra type information, but fixes the problem | |
func workaround<AnimalType>(name: String) -> AnimalType where AnimalType: Named { | |
return AnimalType(name: name) | |
} | |
extension AnimalSpecification where AnimalType: Named { | |
func animal(named name: String) -> AnimalType { | |
return workaround(name: name) | |
} | |
} | |
class Dog: Animal, Named { | |
let name: String | |
required init(name: String) { | |
self.name = name | |
super.init() | |
} | |
} | |
struct Kennel: AnimalSpecification { | |
typealias AnimalType = Dog | |
} | |
let kennel = Kennel() | |
kennel.animal(named: "Brian") |
This file contains hidden or 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
import Foundation | |
// Removing the subclassing from NSObject fixes the problem | |
class Animal: NSObject { | |
} | |
protocol Named { | |
init(name: String) | |
} | |
protocol AnimalSpecification { | |
associatedtype AnimalType: Animal | |
} | |
extension AnimalSpecification where AnimalType: Named { | |
func animal(named name: String) -> AnimalType { | |
return AnimalType(name: name) | |
} | |
} | |
class Dog: Animal, Named { | |
let name: String | |
required init(name: String) { | |
self.name = name | |
super.init() | |
} | |
} | |
struct Kennel: AnimalSpecification { | |
typealias AnimalType = Dog | |
} | |
let kennel = Kennel() | |
kennel.animal(named: "Brian") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This produces this error:
Removing
NSObject
as superclass fromAnimal
makes it work as expected