Skip to content

Instantly share code, notes, and snippets.

@cdmckay
Created January 4, 2017 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdmckay/2c0d20cbd73cffd85d08e0dee0953e72 to your computer and use it in GitHub Desktop.
Save cdmckay/2c0d20cbd73cffd85d08e0dee0953e72 to your computer and use it in GitHub Desktop.
templateService.getById(id) match {
case Found(template) => ...
case NotFound(message) => // message contains a nice (consistent message)
}
for {
checklist <- checklistService.getById(checklistId).toRight("checklist").right
template <- templateService.getById(checklist.template.id).toRight
} yield {
...
}
// It can also have a toOption if needed, although I'm not sure when that would be used
templateService.getById(id).toOption match {
case Some(tmpl) =>
case None =>
}
// Alternatively, we could use a helper method like toEither:
for {
checklist <- checklistService.getById(checklistId).toRight("checklist").right
template <- toEither(templateService.getById(checklist.template.id)) // converts to Either[String, Template]
} yield {
...
}
// I think this might be the best
@ShurikAg
Copy link

ShurikAg commented Jan 4, 2017

The only comment I have about it, in the last option, can we do templateService.getById(checklist.template.id).toEither?

toOption could definitely be handy.

@cdmckay
Copy link
Author

cdmckay commented Jan 4, 2017

Yeah, there's a way to make extension methods like that (it's called the Pimp My Library pattern)

@ShurikAg
Copy link

ShurikAg commented Jan 4, 2017

so, what are these: checklist <- checklistService.getById(checklistId).toRight("checklist").right? Example of an old way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment