Skip to content

Commit

Permalink
doc: metion for the Java 21's new switch statement in document
Browse files Browse the repository at this point in the history
attach the code directly in the doc instead of putting it in `ActorDocTest.java` because we don't support java-jdk-21 now
  • Loading branch information
duobei authored and He-Pin committed Jan 22, 2024
1 parent 2431eb4 commit d282d5e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/src/main/paradox/actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit d282d5e

Please sign in to comment.