Cannot convert value of type 'AnyPublisher<Self.Output, Self.Failure>' to expected argument type 'AnyPublisher<T, any Error>'
import Combine
extension Publisher {
func someFunction<T>() -> AnyPublisher<T, Error> {
// some internal code
eraseToAnyPublisher() // COMPILE ERROR HERE
// return the correct thing
}
}
So, if you comment out the eraseToAnyPublisher()
, everything compiles fine.
So, why is this an issue?
It's internal code, it's not even DOING anything in the function.
Ah. It's confusing.
Here's the fix - add where Output == T, Failure == Error
extension Publisher {
func someFunction<T>() -> AnyPublisher<T, Error> where Output == T, Failure == Error {
eraseToAnyPublisher() // No more compile error
// return the correct thing
}
}
We need to tell the Publisher extension to map itself to the correct types, T
and Error
.
EVEN THOUGH Failure: Error
is already defined on Publisher.
And EVEN THOUGH we aren't RETURNING eraseToAnyPublisher()
Shrug.
I don't know why.
Let's just say, it must be the compiler helping us out down the line, and joining up all the dots and craziness with generics everywhere
ALSO, you can't add where Output == T, Failure == Error
to the extension Publisher where Output == T, Failure == Error
It has to be added to the FUNCTION.
Because..
extension Publisher
has no idea about the genericT
. The generic is declared on the function withfunc someFunction<T>