From 65a4bbbbe39d1aa2c45a6c7f8f35d83e39573732 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 24 Sep 2026 11:54:48 +0000 Subject: [PATCH] patterns: run Guards, NullCase and MatchException demos on 21 too; add switch-on-long check Adds broken/SwitchOnLong.java and regenerates transcripts so every 'runs on 21, 25 and 27' claim has a transcript. Additive commit. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a --- patterns/broken/SwitchOnLong.java | 10 ++++++++++ patterns/output/02-guards-and-dominance.txt | 8 ++++++++ patterns/output/03-record-patterns.txt | 4 ++++ patterns/output/06-null-and-unnamed.txt | 6 ++++++ patterns/output/07-primitive-patterns.txt | 20 ++++++++++++++++++++ patterns/run.sh | 7 ++++--- 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 patterns/broken/SwitchOnLong.java diff --git a/patterns/broken/SwitchOnLong.java b/patterns/broken/SwitchOnLong.java new file mode 100644 index 0000000..f9efdca --- /dev/null +++ b/patterns/broken/SwitchOnLong.java @@ -0,0 +1,10 @@ +public class SwitchOnLong { + public static void main(String[] args) { + long n = 1L; + String s = switch (n) { // switch on long: not allowed without the preview + case 0L -> "zero"; + default -> "other"; + }; + System.out.println(s); + } +} diff --git a/patterns/output/02-guards-and-dominance.txt b/patterns/output/02-guards-and-dominance.txt index 35316ae..2c3f371 100644 --- a/patterns/output/02-guards-and-dominance.txt +++ b/patterns/output/02-guards-and-dominance.txt @@ -1,3 +1,11 @@ +$ javac src/Guards.java && java Guards (21.0.10+7-Ubuntu-124.04) +empty string +short string: abc +long string: abcdefgh +negative -4 +int 12 +other + $ javac src/Guards.java && java Guards (25.0.4.1+1-LTS) empty string short string: abc diff --git a/patterns/output/03-record-patterns.txt b/patterns/output/03-record-patterns.txt index ead4310..7a24658 100644 --- a/patterns/output/03-record-patterns.txt +++ b/patterns/output/03-record-patterns.txt @@ -22,6 +22,10 @@ box of string hi box of int 7 box of 2.5 +$ javac src/MatchExceptionDemo.java && java MatchExceptionDemo (21.0.10+7-Ubuntu-124.04) +MatchException: java.lang.IllegalStateException: accessor blew up +cause: java.lang.IllegalStateException: accessor blew up + $ javac src/MatchExceptionDemo.java && java MatchExceptionDemo (25.0.4.1+1-LTS) MatchException: java.lang.IllegalStateException: accessor blew up cause: java.lang.IllegalStateException: accessor blew up diff --git a/patterns/output/06-null-and-unnamed.txt b/patterns/output/06-null-and-unnamed.txt index 58949a9..c06a1f1 100644 --- a/patterns/output/06-null-and-unnamed.txt +++ b/patterns/output/06-null-and-unnamed.txt @@ -1,3 +1,9 @@ +$ javac src/NullCase.java && java NullCase (21.0.10+7-Ubuntu-124.04) +without case null: NullPointerException +with case null: null! +case null, default: null or something else +instanceof with null: false + $ javac src/NullCase.java && java NullCase (25.0.4.1+1-LTS) without case null: NullPointerException with case null: null! diff --git a/patterns/output/07-primitive-patterns.txt b/patterns/output/07-primitive-patterns.txt index 220a799..06f007a 100644 --- a/patterns/output/07-primitive-patterns.txt +++ b/patterns/output/07-primitive-patterns.txt @@ -12,6 +12,26 @@ preview/PrimitiveInstanceof.java:3: error: primitive patterns are a preview feat (use --enable-preview to enable primitive patterns) 1 error +$ javac broken/SwitchOnLong.java (21.0.10+7-Ubuntu-124.04) +broken/SwitchOnLong.java:4: error: selector type long is not allowed + String s = switch (n) { // switch on long: not allowed without the preview + ^ +1 error + +$ javac broken/SwitchOnLong.java (25.0.4.1+1-LTS) +broken/SwitchOnLong.java:4: error: primitive patterns are a preview feature and are disabled by default. + String s = switch (n) { // switch on long: not allowed without the preview + ^ + (use --enable-preview to enable primitive patterns) +1 error + +$ javac broken/SwitchOnLong.java (27+35) +broken/SwitchOnLong.java:4: error: primitive patterns are a preview feature and are disabled by default. + String s = switch (n) { // switch on long: not allowed without the preview + ^ + (use --enable-preview to enable primitive patterns) +1 error + $ javac --enable-preview --release 25 preview/PrimitiveInstanceof.java && java --enable-preview PrimitiveInstanceof (25.0.4.1+1-LTS) 100 fits in a byte: 100 1000 fits in a short: 1000 diff --git a/patterns/run.sh b/patterns/run.sh index 6668a49..5072a59 100755 --- a/patterns/run.sh +++ b/patterns/run.sh @@ -31,10 +31,10 @@ all3() { local fn="$1"; shift; [ -n "$JDK21" ] && { "$fn" "$JDK21" "$@"; echo; } { all3 cr src/TypePatterns.java TypePatterns; } > "$OUT/01-type-patterns.txt" # 02 - guards and dominance -{ both cr src/Guards.java Guards; echo; both co broken/Dominated.java; echo; both co broken/GuardBeforeGeneral.java; } > "$OUT/02-guards-and-dominance.txt" +{ all3 cr src/Guards.java Guards; echo; both co broken/Dominated.java; echo; both co broken/GuardBeforeGeneral.java; } > "$OUT/02-guards-and-dominance.txt" # 03 - record patterns, and what happens when an accessor throws -{ all3 cr src/RecordPatterns.java RecordPatterns; echo; both cr src/MatchExceptionDemo.java MatchExceptionDemo; } > "$OUT/03-record-patterns.txt" +{ all3 cr src/RecordPatterns.java RecordPatterns; echo; all3 cr src/MatchExceptionDemo.java MatchExceptionDemo; } > "$OUT/03-record-patterns.txt" # 04 - exhaustiveness: the compiler's check, and what separate compilation can still break { all3 cr src/SealedExhaustive.java SealedExhaustive; echo @@ -51,10 +51,11 @@ all3() { local fn="$1"; shift; [ -n "$JDK21" ] && { "$fn" "$JDK21" "$@"; echo; } done; } > "$OUT/05-separate-compilation.txt" # 06 - null and unnamed patterns -{ both cr src/NullCase.java NullCase; echo; all3 cr src/Unnamed.java Unnamed; } > "$OUT/06-null-and-unnamed.txt" +{ all3 cr src/NullCase.java NullCase; echo; all3 cr src/Unnamed.java Unnamed; } > "$OUT/06-null-and-unnamed.txt" # 07 - primitive patterns (preview) { for f in PrimitiveInstanceof; do both co preview/$f.java; done; echo + all3 co broken/SwitchOnLong.java; echo for f in PrimitiveInstanceof PrimitiveSwitch PrimitiveInRecord PrimitiveEdges; do both crp preview/$f.java $f; echo; done both crp preview/BoxedToNarrower.java BoxedToNarrower; } > "$OUT/07-primitive-patterns.txt" { o="$(mk)"; "$JDK25/bin/javac" --enable-preview --release 25 -d "$o" preview/PrimitiveSwitch.java 2>&1 | grep -v '^Note:'