I have the following methods:
public class Example {
public CompletableFuture<Bar<InputStream>> methodA(CompletableFuture<InputStream> callback) {
return methodB(new Foo(), this::mapper, callback);
}
private <T> CompletableFuture<Bar<T>> methodB(Foo request, Function<Foo, Bar<T>> mapper, CompletableFuture<Bar<T>> callback) {
return null;
}
private Bar<InputStream> mapper(Foo foo) {
return new Bar<>();
}
}
The call from "methodA" to "methodB" cannot compile because:
Error:(9, 16) java: method methodB in class example.Example cannot be applied to given types;
required: example.Foo,java.util.function.Function<example.Foo,example.Bar<T>>,java.util.concurrent.CompletableFuture<example.Bar<T>>
found: example.Foo,this::mapper,java.util.concurrent.CompletableFuture<java.io.InputStream>
reason: cannot infer type-variable(s) T
(argument mismatch; java.util.concurrent.CompletableFuture<java.io.InputStream> cannot be converted to java.util.concurrent.CompletableFuture<example.Bar<T>>)
I Understand (sort of) why the error is happening. The compiler seems to be confusing the type params of the future with that of the function. What I'm not sure about is how to work around this? The body of "methodB" is correct and works.
Interestingly if you call
methodB(new Foo(), this::mapper, new CompletableFuture<>());
It works just fine due to erasure.
The body of "methodB" is correct and works.
Even though you say this, the last parameter of methodB is essentially why your code doesn't compile. For some reason, the type of callback is a CompletableFuture<Bar<T>>, but the callback parameter passed to methodA is a CompletableFuture<InputStream>, and an InputStream is not a Bar<T>.
For this to compile, you can change methodB to the following:
private <T> CompletableFuture<Bar<T>> methodB(Foo request, Function<Foo, Bar<T>> mapper, CompletableFuture<T> callback) {
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With