我希望有一种像Swift 3一样的通用方法:
public protocol Callable {
associatedtype In : CVarArg
associatedtype Out : CVarArg
}
public struct IntCallable : Callable {
public typealias In = Int
public typealias Out = Double
public typealias FunctionalBlock = @convention(c) (In) -> Out
public func call(_ block: FunctionalBlock) { /* do stuff */ }
}
所以我希望它看起来更像这样:
public protocol Callable {
associatedtype In : CVarArg
associatedtype Out : CVarArg
typealias FunctionalBlock = @convention(c) (In) -> Out
}
public struct IntCallable : Callable {
public typealias In = Int
public typealias Out = Double
}
public extension Callable {
public func call(_ block: FunctionalBlock) { /* do stuff */ }
}
然而,我收到错误:
'(Self.In) -> Self.Out' is not representable in Objective-C, so it cannot be used with '@convention(c)'
我可以对In / Out关联类型进行约束,这将允许我声明FunctionalBlock的通用形式吗?没有@convention(c)
它工作正常,但我需要它才能形成一个C函数调用。