带闭包的数组扩展
Extension for Array with Closures
我有一个数组包含闭包和下一种闭包:
typealias FuncT = (()->Void)
我想向包含我的函数的数组添加扩展方法:
extension Array where Element : FuncT {
func execAll() {
self.forEach { (f) in
f()
}
}
}
我收到编译错误:
Type 'Element' constrained to non-protocol, non-class type 'FuncT'
(aka '() -> ()')
如何向包含我的函数的数组添加方法?
约束 where A : B
将 A
限制为 class B
的子class,或者符合 协议 B
的类型。函数类型是值类型但不是 classes,并且不能符合协议。
你需要的是“同类型要求”where A == B
。在你的情况下:
extension Array where Element == FuncT { ... }
我有一个数组包含闭包和下一种闭包:
typealias FuncT = (()->Void)
我想向包含我的函数的数组添加扩展方法:
extension Array where Element : FuncT {
func execAll() {
self.forEach { (f) in
f()
}
}
}
我收到编译错误:
Type 'Element' constrained to non-protocol, non-class type 'FuncT' (aka '() -> ()')
如何向包含我的函数的数组添加方法?
约束 where A : B
将 A
限制为 class B
的子class,或者符合 协议 B
的类型。函数类型是值类型但不是 classes,并且不能符合协议。
你需要的是“同类型要求”where A == B
。在你的情况下:
extension Array where Element == FuncT { ... }