如何使用类型约束调用协议扩展默认实现
How to invoke protocol extension default implementation with type constraints
考虑以下示例:
class ManObj {
func baseFunc() {
print("ManObj baseFunc")
}
}
class SubObj: ManObj {
}
protocol Model {
}
extension Model { // This is protocol extension
func someFunc() { // Protocol extension default implementation
(self as! ManObj).baseFunc()
print("Model implementation")
}
}
extension SubObj: Model {
func someFunc() {
print("SubObj Implementation")
}
}
let list = SubObj()
list.someFunc() // static dispatching
let list2: Model = SubObj()
list2.someFunc() // dynamic dispatching
输出很好:
SubObj Implementation
ManObj baseFunc
Model implementation
但我不喜欢 (self as! ManObj).baseFunc()
.
行中的转换
事实上,我只打算将Model
协议应用于ManObj
的子类。 (但并非 ManObj
的所有子类都是 Model
!)因此,我尝试将 Model
更改为:
extension Model where Self: ManObj {
func someFunc() {
self.baseFunc() // No more casting needed!
print("Model implementation")
}
}
但是我遇到了错误:
list2.someFunc()
<- error: 'Model' is not a subtype of 'ManObj'
那么,在我将 Model
限制为 where Self: ManObj
之后,有没有办法让我从 list2
触发 Model.someFunc
?
为类型转换创建一个空的class
class ManObj {
func baseFunc() {
print("ManObj baseFunc")
}
}
class SubObj: ModelCaster {
func someFunc() {
print("SubObj Implementation")
}
}
protocol Model {
}
extension Model where Self: ModelCaster { // This is protocol extension
func someFunc() { // Protocol extension default implementation
print("Model implementation")
}
}
class ModelCaster: ManObj, Model{
}
let list = SubObj()
list.someFunc() //SubObj Implementation
let list2: ModelCaster = SubObj()
list2.someFunc() //Model implementation
考虑以下示例:
class ManObj {
func baseFunc() {
print("ManObj baseFunc")
}
}
class SubObj: ManObj {
}
protocol Model {
}
extension Model { // This is protocol extension
func someFunc() { // Protocol extension default implementation
(self as! ManObj).baseFunc()
print("Model implementation")
}
}
extension SubObj: Model {
func someFunc() {
print("SubObj Implementation")
}
}
let list = SubObj()
list.someFunc() // static dispatching
let list2: Model = SubObj()
list2.someFunc() // dynamic dispatching
输出很好:
SubObj Implementation
ManObj baseFunc
Model implementation
但我不喜欢 (self as! ManObj).baseFunc()
.
事实上,我只打算将Model
协议应用于ManObj
的子类。 (但并非 ManObj
的所有子类都是 Model
!)因此,我尝试将 Model
更改为:
extension Model where Self: ManObj {
func someFunc() {
self.baseFunc() // No more casting needed!
print("Model implementation")
}
}
但是我遇到了错误:
list2.someFunc()
<- error: 'Model' is not a subtype of 'ManObj'
那么,在我将 Model
限制为 where Self: ManObj
之后,有没有办法让我从 list2
触发 Model.someFunc
?
为类型转换创建一个空的class
class ManObj {
func baseFunc() {
print("ManObj baseFunc")
}
}
class SubObj: ModelCaster {
func someFunc() {
print("SubObj Implementation")
}
}
protocol Model {
}
extension Model where Self: ModelCaster { // This is protocol extension
func someFunc() { // Protocol extension default implementation
print("Model implementation")
}
}
class ModelCaster: ManObj, Model{
}
let list = SubObj()
list.someFunc() //SubObj Implementation
let list2: ModelCaster = SubObj()
list2.someFunc() //Model implementation