Remove fixme part about guards and pattern matching from StyleGuide (#30787)
This commit is contained in:
parent
33052e3abc
commit
4d14c6f977
2 changed files with 3 additions and 47 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue