This is not really Akka related question because the main problem here is pattern matching and complying with return type of receive
. receive
is a partial function that has this return type: PartialFunction[Any, Unit]
. So you don't really have to return anything specific from your if/else
statement. Thus you just can log
without any sort of break
.
To improve style you can use one of these ways to write the pattern matching expression:
scala> case class Alarm(msg: String)
defined class Alarm
scala> Alarm(null) match {
| case Alarm(null) => "Null"
| case Alarm(_) => "NOT null"
| }
res1: String = Null
scala> Alarm(null) match {
| case Alarm(smth) if(smth == null) => "Null"
| case Alarm(_) => "NOT null"
| }
res2: String = Null
The first option is less verbose and better in this case.
offtopic: avoid using null
, use Option[T]
instead or case object
.
Additional (using case object):
scala> sealed trait AlarmLike
defined trait AlarmLike
scala> case class Alarm(msg: String) extends AlarmLike
defined class Alarm
scala> case object EmptyAlarm extends AlarmLike
defined module EmptyAlarm
scala> val l: List[AlarmLike] = List(Alarm("Ahtung!"), EmptyAlarm)
l: List[AlarmLike] = List(Alarm(Ahtung!), EmptyAlarm)
scala> l map { case Alarm(desc) => desc; case EmptyAlarm => "EmptyAlarm" }
res0: List[String] = List(Ahtung!, EmptyAlarm)