Consider the following examples
Consumer<Long> f1 = new Consumer<>() {
@Override
public void accept(Long value) {
if (value < 5) {
this.accept(value + 1); //this refers to the anonymous context.
}
}
}
Consumer<Long> f2 = (value) -> {
if (value < 5) {
this.accept(value + 1); //this refers to outer context and the anonymous function is not referable.
}
};
It is seen that this
is not provided for the lambda f2
but is given for the more explicitly written anonymous Consumer implementation f1
.
Why is this so? What would be the language design difficulty in providing this
for the lambda body?