Flexible constructor bodies: Odd treatment of "return" in lambdas.
Ella Ananeva
ella.ananeva at oracle.com
Wed Dec 4 01:03:16 UTC 2024
Hi team,
For some reason, compiler rejects “return” in lambda if it’s declared in the early construction context. But a similar lambda without “return” is accepted. Is this by design?
interface Foo { int get(); }
class Test {
Test() {
Foo lmb = () -> 1; //is okay
Foo lmb2 = () -> {
return 1; //fails with a compilation error
};
super();
Foo lmb3 = () -> {
return 1; //is okay
};
}
}
public void main() {
new Test();
}
Main.java:13: error: 'return' not allowed before explicit constructor invocation
return 1;
java --version
openjdk 24-ea 2025-03-18
OpenJDK Runtime Environment (build 24-ea+24-2960)
OpenJDK 64-Bit Server VM (build 24-ea+24-2960, mixed mode, sharing)
Thank you,
Ella
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241204/76bc2eb1/attachment.htm>
More information about the amber-dev
mailing list