1

This question has been asked a few times, beneath my question are the linked other questions with an explanation why it didn't worked out well.

This is my code:

protocol SomeProtocol {
    init()
}

protocol Initializable where Self: SomeProtocol {}

protocol ProtocolA {
    associatedtype A: Initializable
}

extension ProtocolA {
    func a() {
        A()
    }
}

The error is:

Non-nominal type 'Self.A' does not support explicit initialization

I do not understand why not. The associated type A always is a type of SomeProtocol, which has the initializer.

Tried: non-nominal type X does not support explicit initialization : Type 'Self.A' has no member 'init' Swift 4: Non-nominal type 'T' does not support explicit initialization when converting Objective-c to Swift : whole something else Swift 4: "Non-nominal type 'T' does not support explicit initialization" : I think the compiler doesn't really have the know the exact type.

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • In this case in the `where` clause `Self` must be constrained to a concrete type, not a protocol. A protocol is not a generic. In some of the linked questions generics are used which are also concrete types at the moment the method is called. – vadian Nov 24 '18 at 19:50
  • 1
    In Swift 4.2, superclass constrained protocols aren't properly implemented yet – compare https://stackoverflow.com/q/50913244/2976878 & https://stackoverflow.com/a/50647762/2976878. In the latest master snapshot, your code works as expected. – Hamish Nov 28 '18 at 09:51
  • 1
    But until then, you can just use the short-form equivalent: `protocol Initializable : SomeProtocol {}` – Hamish Nov 28 '18 at 09:55
  • @Hamish Thank you for (again) notifying me about improvements in later releases of Swift :) – J. Doe Nov 28 '18 at 12:09

0 Answers0