I think I recently could not overcome this limitation.
I was thinking to make a nice wrapper for a simple case.
protocol Message {
mutating func set(value: String) throws
}
protocol Proto {
func enrich(_ message: inout some Message) throws
}
struct SimpleWrapper: Proto {
let closure: (inout some Message) throws -> () // `- error: 'some' cannot appear in parameter position in result type '(inout some Message) throws -> ()'
init(closure: @escaping ((inout some Message) throws -> ())) {
self.closure = closure
}
func enrich(_ message: inout some Message) throws {
try self.closure(&message)
}
}
I would like to continue use some rather than any because if use any inout parameter can be replaced with other type...
I wonder if someone could help me finding a good solution.
The first key understanding to obtain is that generic functions cannot be stored as closures. You can only store specialized closures. I think all potential solutions will flow from that.
I looked at the sample but unfortunately that doesn't suit my needs in full. I probably have to extend it a bit to provide the whole picture. I provide Proto as a part of configuration and use it into pluggable code.
Therefore the Proto doesn't have associatedtype Message and even doesn't know which types will be provided to it.
Currently I always just implement this Proto in client code:
// configuration
struct Configuration {
let enricher: any Proto
init(enricher: any Proto) {...}
}
// Client code usage:
struct MyEnricher: Proto {
// ...
func enrich(_ message: inout some Message) throws {
message.set(...)
}
}
Configuration(MyEnricher())