From 4d14c6f97729b1fe05b971d98fc3bc889ce9a98e Mon Sep 17 00:00:00 2001 From: Muskan Gupta Date: Mon, 25 Oct 2021 21:30:41 +0530 Subject: [PATCH] Remove fixme part about guards and pattern matching from StyleGuide (#30787) --- .../akka/typed/StyleGuideDocExamples.scala | 39 ++----------------- .../src/main/paradox/typed/style-guide.md | 11 ------ 2 files changed, 3 insertions(+), 47 deletions(-) diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StyleGuideDocExamples.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StyleGuideDocExamples.scala index 8135746549..86bdfa37aa 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StyleGuideDocExamples.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StyleGuideDocExamples.scala @@ -460,46 +460,13 @@ object StyleGuideDocExamples { final case class GetValue(replyTo: ActorRef[Value]) extends Command final case class Value(n: Int) //#messages-sealed - - def apply(countDownFrom: Int, notifyWhenZero: ActorRef[Done]): Behavior[Command] = - new CountDown(notifyWhenZero).counterWithGuard(countDownFrom) } - private class CountDown(notifyWhenZero: ActorRef[Done]) { + class CountDown() { import CountDown._ - private def counterWithGuard(remaining: Int): Behavior[Command] = { - //#pattern-match-guard - // no exhaustiveness check because of guard condition - // FIXME not true anymore since Scala 2.13.5 - Behaviors.receiveMessagePartial { - case Down if remaining == 1 => - notifyWhenZero.tell(Done) - zero - case Down => - counter(remaining - 1) - } - //#pattern-match-guard - } - - @nowarn - private def counter(remaining: Int): Behavior[Command] = { - //#pattern-match-without-guard - // `@unchecked` for Scala 3, which doesn't support @nowarn - Behaviors.receiveMessage(x => - (x: @unchecked) match { - case Down => - if (remaining == 1) { - notifyWhenZero.tell(Done) - zero - } else - counter(remaining - 1) - }) - //#pattern-match-without-guard - } - //#pattern-match-unhandled - private val zero: Behavior[Command] = { + val zero: Behavior[Command] = { Behaviors.receiveMessage { case GetValue(replyTo) => replyTo ! Value(0) @@ -513,7 +480,7 @@ object StyleGuideDocExamples { @nowarn object partial { //#pattern-match-partial - private val zero: Behavior[Command] = { + val zero: Behavior[Command] = { Behaviors.receiveMessagePartial { case GetValue(replyTo) => replyTo ! Value(0) diff --git a/akka-docs/src/main/paradox/typed/style-guide.md b/akka-docs/src/main/paradox/typed/style-guide.md index c2f64e772a..6f8ac4cf23 100644 --- a/akka-docs/src/main/paradox/typed/style-guide.md +++ b/akka-docs/src/main/paradox/typed/style-guide.md @@ -390,17 +390,6 @@ in the pattern match and return `Behaviors.unhandled`. Scala : @@snip [StyleGuideDocExamples.scala](/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StyleGuideDocExamples.scala) { #pattern-match-unhandled } -One thing to be aware of is the exhaustiveness check is not enabled when there is a guard condition in any of the -pattern match cases. - -Scala -: @@snip [StyleGuideDocExamples.scala](/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StyleGuideDocExamples.scala) { #pattern-match-guard } - -Therefore, for the purposes of exhaustivity checking, it is be better to not use guards and instead move the `if`s after the `=>`. - -Scala -: @@snip [StyleGuideDocExamples.scala](/akka-actor-typed-tests/src/test/scala/docs/akka/typed/StyleGuideDocExamples.scala) { #pattern-match-without-guard } - It's recommended to use the `sealed` trait and total functions with exhaustiveness check to detect mistakes of forgetting to handle some messages. Sometimes that can be inconvenient and then you can use a `PartialFunction` with `Behaviors.receivePartial` or `Behaviors.receiveMessagePartial`