我對Swift很陌生,最近發現您不能從Swift中的泛型繼承,例如,替代繼承自Swift中的泛型參數
class MyClass<T> : T {}
在Swift 3中無效(請參閱問題this question)。
這是我希望用上述構建來解決這個問題:
protocol Backend {
func operationA(operand: Int)
}
class ConcreteBackend : Backend {
func operationA(operand: Int) {
// ...
}
// Some other functions and/or custom initializers
// ...
}
class EnhancedBackend<T : Backend> : T {
override func operationA(operand: Int) {
// do something smart here
super.operationA(operand: modifiedOperand)
}
}
基本上EnhancedBackend
不靈光一現用的operationA
的輸入,然後把它傳遞給實際執行的Backend
。 我在這裏使用繼承而不是組合,因爲ConcreteBackend
可能有一些公共屬性,函數和初始值設定項沒有在協議中指定(因爲它們只與具體的實現有關),我還想用EnhancedBackend
公開。 沒有繼承,這是不可能的。
A C++實現可能看起來像
// Using concepts here instead of protocols
class ConrecteBackend {
public:
void operationA(int operand) { .... }
}
template<class T>
class EnhancedBackend : public T {
using Base = T;
public:
// Ensure T is a model of the Backend concept
static_assert(isModelOfConceptBackend<T>::value,
"Template parameter is not a model of concept Backend");
// Ensure all constructors of Base can be used
template<class ...Args, typename = std::enable_if_t<
std::is_constructible<Base, Args...>::value>>
inline EnhancedBackend(Args &&...args) : Base(std::forward<Args>(args)...) {}
void operationA(int operand) {
// ...
Base::operationA(operand);
}
};
使用C
所以++這是相當簡單的解決這個問題。但目前我還不知道如何使用(純)Swift 3實現。
你檢查出這個答案嗎? http://stackoverflow.com/questions/27508284/swift-inherit-from-generic-type?noredirect=1&lq=1 – Grimxn
@Grimxn問答現場,但已經鏈接到OP上面的第一個代碼片段之後:) – dfri
@dfri - 哎呀! :)只是讀到底部 - OP甚至評論這個問題! – Grimxn