I have a functional interface that extends standard jdk function to simply the generic types. Now I want to combine two functions using andThen which is throwing compiler error
Error:(25, 25) java: method andThen in interface
java.util.function.Function<T,R>cannot be applied to given types;
required:java.util.function.Function<? super ui.instrumentation.api.messaging.Message<R>,? extends V>found:ui.instrumentation.api.transformation.Transformer<T,R>reason: cannot infer type-variable(s) V (argument mismatch;ui.instrumentation.api.transformation.Transformer<T,R>cannot be converted tojava.util.function.Function<? super ui.instrumentation.api.messaging.Message<R>,? extends V>)
Here is the sample code:
public interface Transformer<T,R> extends Function<Message<T>, Message<R>> {
static <T, R> Transformer<T, R> combine2(Transformer<T, R> first, Transformer<T, R> second) {
return first.andThen(second));
}
}
Is there a way to combine functions that extends standard Function interface or is there better way to do this?
combine2function, whenandThendoes literally the same thing, for the more general argument typeFunction?Message<T>, passes it tofirst, gets aMessage<R>back, then somehow passes thatMessage<R>tosecondeven thoughsecondneeds aMessage<T>not aMessage<R>.Function). Thus, you need to re-implement everything, not onlyandThen, even trivial functions likeFunction.identity()can’t be used asTransformer. Learn the lesson from the other’s mistakes; despite having the same functional signature, you can’t use aTransformer<T,T>, where aUnaryOperator<Message<T>>is required (another example of not allowing the base typeFunction)…