Protocol Types Cannot Conform to Protocols

From Protocol Types Cannot Conform to Protocols document:

The word "Currently" is encouraging. Are there plans to make amendments here? For example have a notion of restricted protocol that can't have initialisers / static members, which would allow the existential of type P conform to P.

bikeshedding:


Edited:

Wrong example
restricted protocol Animal {
    init() // 🛑 Error, can't have initializers in restricted protocols
    static func foo() // 🛑 Error, can't static members in restricted protocols
    func makeNoise()
}

func declareAnimalSpecies<T: Animal>(_ animal: T) {
    animal.makeNoise()
}

var animal: Animal = Dog()
declareAnimalSpecies(animal) // ✅

Corrected example:

restricted protocol CanMakeNoise {
    func makeNoise()
}

struct Dog: CanMakeNoise {
    func makeNoise() { print("Woof") }
}

func declareAnimalSpecies<T: CanMakeNoise>(_ animal: T) {
    animal.makeNoise()
}

extension Array: CanMakeNoise where Element: CanMakeNoise {
    func makeNoise() {
        forEach { element in
            element.makeNoise()
        }
    }
}

var animal: CanMakeNoise = Dog()
declareAnimalSpecies([animal]) // ✅
// currently gives error

Or perhaps a version of "frozen" attribute, which if apply to a protocol that doesn't have inits/statics would make it "restricted".

1 Like

Doesn't this example already work because of existential unwrapping?

1 Like

Thank you, corrected example above.

1 Like

No, it was meant to signify that this is a design decision, not a law of the universe, which is a distinction (but not a contradiction) from the preceding paragraph.