我的代码中有一些协议层次结构,我有定义我使用的对象的协议和定义与这些对象一起使用的函数的协议.
对象协议由其他对象协议继承,这些协议为原始协议添加了更多功能,使用它们的功能也是如此.问题是我找不到专门化函数的方法来只接受继承的参数.
这里有一些代码来澄清我正在尝试做的事情:
protocol A {
var foo: String { get set }
}
protocol B: A {
var bar: String { get set }
}
struct Test: B {
var foo: String = "foo"
var bar: String = "bar"
}
protocol UseAProtocol {
static func use<T: A>(_ obj: T)
}
protocol UseBProtocol: UseAProtocol {
}
extension UseBProtocol {
//If I change the requirement to <T: B> this won't conform to `UseAProtocol`.
static func use<T: A>(_ obj: T) {
print(obj.foo) …Run Code Online (Sandbox Code Playgroud)