You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by he...@apache.org on 2024/01/22 16:55:40 UTC

(incubator-pekko) branch main updated: doc: metion for the Java 21's new switch statement in document

This is an automated email from the ASF dual-hosted git repository.

hepin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-pekko.git


The following commit(s) were added to refs/heads/main by this push:
     new d282d5e153 doc: metion for the Java 21's new switch statement in document
d282d5e153 is described below

commit d282d5e1538821eea129446b090d58ab9c63517a
Author: Luca Zhang <fe...@gmail.com>
AuthorDate: Tue Jan 23 00:44:36 2024 +0800

    doc: metion for the Java 21's new switch statement in document
    
    attach the code directly in the doc instead of putting it in `ActorDocTest.java` because we don't support java-jdk-21 now
---
 docs/src/main/paradox/actors.md | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/docs/src/main/paradox/actors.md b/docs/src/main/paradox/actors.md
index 60057b95f3..44e205bb8b 100644
--- a/docs/src/main/paradox/actors.md
+++ b/docs/src/main/paradox/actors.md
@@ -840,6 +840,41 @@ untyped version. When extending `UntypedAbstractActor` each message is received
 
 @@snip [ActorDocTest.java](/docs/src/test/java/jdocs/actor/ActorDocTest.java) { #optimized }
 
+In addition, Java 21 introduces [powerful pattern matching for switch](https://openjdk.org/jeps/441) supporting any reference type. We can use `switch` instead of `if ... else ...` for conditional branches:
+
+```java
+static class PatternMatchedActor extends UntypedAbstractActor {
+
+    public static class Msg1 {}
+
+    public static class Msg2 {}
+
+    public static class Msg3 {}
+
+    @Override
+    public void onReceive(Object msg) throws Exception {
+      switch(msg) {
+        case Msg1 msg -> receiveMsg1((Msg1) msg);
+        case Msg2 msg -> receiveMsg2((Msg2) msg);
+        case Msg3 msg -> receiveMsg3((Msg3) msg);
+        default _ -> unhandled(msg);
+      }
+    }
+
+    private void receiveMsg1(Msg1 msg) {
+      // actual work
+    }
+
+    private void receiveMsg2(Msg2 msg) {
+      // actual work
+    }
+
+    private void receiveMsg3(Msg3 msg) {
+      // actual work
+    }
+  }
+```
+
 @@@
 
 <a id="actor-reply"></a>


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org