What I would actually do is use the applyOrElse
method on dataFunction
, defining a default response when the partial function is not defined for the given input
def defaultAnswer: T
def receive: Receive = {
case Request(data) =>
sender ! respond(dataFunction.applyOrElse(data, defaultAnswer))
}
If you prefer to wrap your answer in a Option
, there's already a method called lift
that does that for you, as you correctly guessed
def receive: Receive = {
case Request(data) =>
sender ! respond(dataFunction.lift(data))
}
You can even decide not to answer if the function is not defined for data
def receive: Receive = {
case Request(data) =>
dataFunction.lift(data).foreach {
sender ! respond(_)
}
}
foreach
defined on the Option
type runs the code within the closure only if there's some content available (i.e. if you have a Some(...)
)