J. Doe
J. Doe

Reputation: 13033

Method ... in non-final class ... cannot be implemented in a protocol extension because it returns 'Self' and has associated type requirements

This is my code:

protocol Person {
    associatedtype Homework

    static func generate(homeWork: Homework) -> Self
}

extension Person {
    static func generate(homeWork: Homework) -> Self {
        fatalError()
    }
}

// Method 'generate(homeWork:)' in non-final class 'Teacher' 
// cannot be implemented in a protocol extension because it 
// returns 'Self' and has associated type requirements
class Teacher: Person {
    typealias Homework = Int
}

Despite the strange naming of types, I am curious why it isn't possible to conform a non-final class to a protocol which has a method that returns Self and having a associatedtype as parameter type.

Note: I am not asking how to fix this problem (because I can just mark my class final). I want to know why this isn't possible.

I think it is strange. Protocols with associatedtypes and Self is always a struggle ofcourse, but when using one of two (returning Self/associatedtype protocol type in parameter) compiles the code just fine.

Why can't I conform to a protocol which has a default implementation of a method whereas the method has a return type of Self and associatedtype in a parameter, but when only using one of them the code compiles? Why is both a problem when dealing with inherence (because marking the class as final fixes the error message, so it has to be something with inherence)?

Upvotes: 4

Views: 578

Answers (1)

gadu
gadu

Reputation: 1826

Looks like its because of a compiler limitation. Here's some relevant discussion from people on the Swift team: https://twitter.com/_danielhall/status/737782965116141568

The original tweet shows what would happen if we tried to create a subclass from Teacher, the compiler would not be able to identify it as conforming to the Person protocol.

Specifically relevant from the swift team in that thread: "we don't support subtyping when matching protocol requirements. I consider it a missing feature in the language"

So knowing the compiler can't recognize subclasses as conforming to the protocol in these situations they decide to force a final class.

Upvotes: 0

Related Questions