I am unsure how pattern matching knows whether a StatusReply
is a success or error.
val reply = StatusReply.error("my error message")
reply match
case StatusReply.Success(v) => //
case StatusReply.Error(msg) => //
In general, I am not certain how pattern matching chooses which case to use when using extractor objects. The unapply
method in extractor objects has one argument which specifies the type of the object to be deconstructed. Im guessing that pattern matching chooses the case based on type of the argument in the unapply method. StatusReply.Success
's unapply method deconstructs StatusReply[Any] and StatusReply.Error
deconstructs StatusReply[_] . What is the difference between Any
and _
in this situation?
This is the code of the extractor objects of StatusReply from the scala source code of StatusReply.
object Success {
def unapply(status: StatusReply[Any]): Option[Any] =
if (status != null && status.isSuccess) Some(status.getValue)
else None
}
object Error {
def unapply(status: StatusReply[_]): Option[Throwable] =
if (status != null && status.isError) Some(status.getError)
else None
}