Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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")
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")
@nvh

This comment has been minimized.

Copy link
Owner Author

@nvh nvh commented Aug 24, 2016

This produces this error:

screen shot 2016-08-24 at 02 50 25

Removing NSObject as superclass from Animal makes it work as expected

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