[swift-evolution] [Discussion] Inheritance from associated types / protocols as broader templates

mike at ap14.com mike at ap14.com
Mon Oct 30 10:08:50 CDT 2017

I often find myself running up against errors like "Inheritance from non-protocol, non-class type”.

Here’s a contrived example.

public protocol NoiseMechanism : class {
    func makeNoise()

public protocol Animal {
    associatedtype NoiseMaker: NoiseMechanism
    static var numberOfLegs: Int { get }

open class AnimalWrapper<A: Animal> : A.NoiseMaker {

The benefit is that a consuming developer can inject types using one parameter and result in a robust type ecosystem.

This ties into my previous request to have non-class constraints (enums, structs) on protocols and in where clauses. I’m very interested in using protocols to enforce architectural decisions.

What do people generally think about this?  Is a protocol the right construct? Is this something that could be possible in the future, or completely off the table due to complexity, or just not addressed due to low priority?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171030/cad50efd/attachment.html>

More information about the swift-evolution mailing list