使函数参数成为也符合协议的协议类型

Make a function parameter be of a protocol type that also conforms to a protocol

我想制作一个接受协议类型参数的函数,并确保该协议类型也符合另一种协议类型。有什么办法可以做到这一点?还是我必须从根本上重新考虑我的方法?

示例:

// This doesn't extend CaseIterable itself because I would like to use it as a concrete type and not just a generic constraint
protocol MyProtocol {
    /*some protocol stuff*/
}

enum myEnum: MyProtocol, CaseIterable {
    /*some enum stuff*/
}

func<T: CaseIterable>(_ myEnum: MyProtocol.Type) 
where MyProtocol.Type: CaseIterable {
    myEnum.allCases //   <--- This is what I would like to do
}

您需要更改函数签名以使用泛型类型约束作为输入参数的类型,并将泛型类型参数限制为 MyProtocolCaseIterable.

func enumFunc<T: MyProtocol & CaseIterable>(_ myEnum: T.Type) {
    myEnum.allCases //   <--- This is what I would like to do
}