I am trying to mimic the java's Function<T,R> by writing below code :
interface SFunction<T, G> { G apply(T t); default <Q> SFunction<T, Q> andThen(SFunction<? super G, ? extends Q> after) { return (T t) -> { return after.apply(apply(t)); }; // return new SFunction<T, Q>() { // @Override // public Q apply(T t) { // return after.apply(apply(t)); // } // }; } }
when I am writing below code in andThen
, it's working fine (pointing to apply(T t)
of interface) and perfectly chaining the other functional implementations
snippet A return (T t) -> {
return after.apply(apply(t));
};
But when writing below snippet, it's falling into recursion, logically that's correct
snippet B return new SFunction<T, Q>() {
@Override
public Q apply(T t) {
return after.apply(apply(t));
}
};
, but why in snippet A, after.apply(apply(t))
is calling outer apply(T t)
even though internally java implement the anonymous implementation.
Isn't snippet A and B same internally and logically?
Thanks