I want to have a generic way of doing something like in 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 */ }
}
So I'd like it to look more like this:
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 */ }
}
However, I get the error:
'(Self.In) -> Self.Out' is not representable in Objective-C, so it cannot be used with '@convention(c)'
Is there any constraint I can place on the In/Out associatedtypes that will allow my to declare the generic form of the FunctionalBlock? It works fine without @convention(c)
, but I need it in order to form a C function call.