I have an object that had an optional field on it, and on certain point of my code I need to have this field, so I did something like this:
def updateReport(task: Task): Future[Task] = for {
taskResponse <- Future {
task.response.getOrElse(throw NoExpectedFieldsException(s"expected to have Response for taskId: ${task.taskId}"))
}
res <- reportService.updateReportWithResponse(taskResponse) map (_ => task)
} yield res
task looks something like:
Task(taskId: String, ... , response: Option[Response])
is there a more elegant way of doing that?
thought maybe to add function:
private def extractOpt[T](elem: Option[T]) = {
elem.getOrElse(throw NoExpectedFieldsException(s"expected to have element"))
}
but that just moves the ugliness somewhere else...
Task is a case class I created to be able to create differet type of tasks, it has a field called taskType that determain which type of task it is, and all the required elements are on the task as options