Why Scala 2 Enumerations are a risk in business-critical applications

Dear Diary,
This is a friendly reminder that using Scala 2 Enumerations in business-critical applications is not a good idea. It’s a well-known problem that pattern matching on Enumerations is not exhaustive. In larger codebases, adding a new entry to the enum can cause runtime errors. Try explaining that to your manager after claiming Scala is super type-safe and that runtime errors are non-existent.
Until you migrate to Scala 3, there are a couple of options:
- Use a sealed trait hierarchy. The downside is that it does not provide all values out of the box.
- Use a library like Enumeratum (https://github.com/lloydmeta/enumeratum), which integrates well with other libraries.
Many mainstream languages don’t even address this issue and rely on awkward if/else statements. While adding an entry may not result in a runtime error, missing a business case might cost even more!
Read more
Contact us