diff options
author | Aliaksei Budavei <32549825+zzzyxwvut@users.noreply.github.com> | 2024-06-09 19:39:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-09 18:39:52 +0200 |
commit | d6b4afb636aed7eaf709eb27c4944a1263a0f887 (patch) | |
tree | 1e516d3cf250a9ae1e29678fd2057793091f4222 /runtime/syntax/testdir/input/java_lambda_expressions.java | |
parent | 477402ecf99b09087c2517398f05e02f082ef192 (diff) |
runtime(java): Exclude lambda expressions from _when_ _switch-case_ label clauses (#14945)
These guard clauses are always boolean expressions, whereas
lambda expressions can only appear in either an assignment,
a casting, or an invocation context.
References:
https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11.1
https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.27
Signed-off-by: Aliaksei Budavei <0x000c70@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
Diffstat (limited to 'runtime/syntax/testdir/input/java_lambda_expressions.java')
-rw-r--r-- | runtime/syntax/testdir/input/java_lambda_expressions.java | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/runtime/syntax/testdir/input/java_lambda_expressions.java b/runtime/syntax/testdir/input/java_lambda_expressions.java index 5c9411e268..75f5af4cc8 100644 --- a/runtime/syntax/testdir/input/java_lambda_expressions.java +++ b/runtime/syntax/testdir/input/java_lambda_expressions.java @@ -4,8 +4,9 @@ import java.lang.annotation.ElementType; import java.util.function.BinaryOperator; import java.util.function.Function; +import java.util.function.Predicate; -class LambdaExpressionsTests +class LambdaExpressionsTests // JDK 21+. { <I1, C1, C2, T1, T2, T3, Z1, Z2, Z3, S1, S2, S3> void test() { // Schönfinkel's functions. @@ -101,4 +102,53 @@ class LambdaExpressionsTests Function<Function<A1, Function<A2, A3>>, Function<Function<A1, A2>, Function<A1, A3>>> { } + + static void echo(Object o) { System.out.println(o); } + + static { + enum Letters { OTHER, ALPHA, BETA } + + Letters other = Letters.OTHER; + + switch (other) { + case Letters alpha when Letters.ALPHA == alpha: + { echo(alpha); break; } + case Letters beta when Letters.BETA == beta: + { echo(beta); break; } + default: { echo(other); } + } + + echo(switch (other) { + case Letters alpha when Letters.ALPHA == alpha + -> alpha; + case Letters beta when Letters.BETA == beta + -> beta; + default -> other; + }); + + switch (null) { + case String str when !"<empty>".equals(switch (str) { + case String str_ when + Predicate.<String>not(text -> + !text.isEmpty()) + .test(str_) + -> "<empty>"; + case String str_ -> str_; + }): { echo(str); break; } + case null: default: { echo("Other"); } + }; + + echo(switch (null) { + case String str when !"<empty>".equals( + switch (str) { + case String str_ when + Predicate.<String>not(text -> + !text.isEmpty()) + .test(str_) + -> "<empty>"; + case String str_ -> str_; + }) -> str; + case null, default -> "Other"; + }); + } } |