Migrating to Moya 11 generates error (see below) when overriding the MoyaProvider initializer with default values.
Error:
Default argument value of type '(Endpoint, (Result
Code:
override init(endpointClosure: @escaping EndpointClosure = MoyaProvider.defaultEndpointMapping,
requestClosure: @escaping RequestClosure = MoyaProvider.defaultRequestMapping,
stubClosure: @escaping StubClosure = MoyaProvider.neverStub,
callbackQueue: DispatchQueue? = nil,
manager: Manager = MoyaProvider<MultiTarget>.defaultAlamofireManager(),
plugins: [PluginType] = [AuthPlugin()],
trackInflights: Bool = false) {
super.init(endpointClosure: endpointClosure,
requestClosure: requestClosure,
stubClosure: stubClosure,
callbackQueue: callbackQueue,
manager: manager,
plugins: plugins,
trackInflights: trackInflights)
}
How to assign a defaultRequestMapping?
Hey @jhrasco, thanks for reporting this. I'm thinking that MoyaProvider.defaultRequestMapping needs to have its RequestResultClosure argument marked as @escaping. What version of Moya are you migrating from?
Hi @SD10 Thank you for a quick response. I'm migrating from version 10.0.2 to 11.
@jhrasco Are you subclassing? This is an odd error message.
The real fix is you need to reference YourProviderSubclass.defaultRequestMapping or resolve MoyaProvider's generic.
It compiles without it but I still think we should resolve the missing @escaping attribute.
The function signature is:
public final class func defaultRequestMapping(for endpoint: Endpoint, closure: RequestResultClosure)
RequestClosure is typed as:
public typealias RequestClosure = (Endpoint, @escaping RequestResultClosure) -> Void
cc @sunshinejr
@SD10 Thanks. Yes, I'm subclassing and your proposed solution,ProviderSubclass.defaultRequestMapping, works. Thank you.
Looks like a typealias for a function is @escaping by default in Swift. So we don't need to mark RequestResultClosure.
But we need the @escaping attribute on RequestClosure because it looks like the @escaping attribute is not inferred by default on a typealias 馃
Bad error message I guess.
It looks like when Endpoint was generic, MoyaProvider was able to resolve the type.
You could use MoyaProvider.defaultRequestMapping in 10.0.2 but now you cannot. You must pass on the generic from the subclass or refer to the subclass itself.
馃挴 I've added a line to Migration Guide in #1570.
Most helpful comment
@jhrasco Are you subclassing? This is an odd error message.
The real fix is you need to reference
YourProviderSubclass.defaultRequestMappingor resolveMoyaProvider's generic.