Best practices of safe pattern matching in a Scala application

Click for: original source

When you start developing your first Scala projects, you may sometimes miss out to address one common problem with pattern matching, and that is error handling. This article by Eugene Krotov teaches how to do it.

In the article, author looks at a few ways to handle the MatchError runtime exception in Scala:

  • Adding a default case _ to catch all unmatched options
  • Using the keyword sealed with the base class or trait for matching options (although this is a workaround not a problem solver)
  • Changing the compiler options to stop compilation whenever there are warnings produced thanks to sealed

The article provides step by step instructions, code examples and more. It reviews the listed ways to address or mitigate the problem. Very good and to the point!

[Read More]

Tags scala programming java