Scala: How to return a result code in a concise way

Dmitry Komanov
2 min readJan 9, 2016

--

First of all, I don’t want to raise a topic about “Exceptions versus Error Codes”. But if you need to return a result code and you use Scala, I have an elegant solution for you to do so.

We have a simple contract:

def processRequest(userId: UUID, requestId: UUID): BusinessResult

Where BusinessResult is plain Java enumeration:

public enum BusinessResult
{
Ok, UserNotFound, RequestNotFound, NotOwner
}

Java-style

The old-school implementation of this method would be:

Pros:

  • Very clear to a reader (even for unfamiliar with Scala)

Cons:

  • In Scala World it’s uncommon to use return keyword
  • Also using of Option.get function is uncommon and considered as a bad practice mostly

Use exceptions internally

Another solution is to use exceptions:

Pros:

  • More concise
  • Not using Option.get function
  • You may extract the code without try..catch and use it for a transition to exception-based error control (if you’re going to that transition)

Cons:

  • Usage of exceptions without actual need

Scala collections

I tried to realize, how to use collections API for such tasks, i.e. using Option.fold method:

It looks concise, but in this approach you will increase indent for each result code.

Either FTW

The common use case for Either is a replacement for Option where None will contain some meaningful value (by convention it’s Left).

We will use for comprehensions on RightProjection of our Either. If we have Left (which means an error) then this value will be returned, otherwise there will be a call to flatMap function with the next RightProjection etc.

Pretty straightforward.

A result type declaration is not necessary, I’ve put it just to show what do we have in the end of the for clause.

Adding some implicit magic…

With some implicit “magic” we can transform this code to a bit more concise form:

All the magic is implicit classes that provides orResult function to create Either from Option and Try and an implicit conversion for Either[T, T] (to not write fold(identity, identity)).

Of course, this approach will work not only with enums but with any type.

Conclusion

So, we made a path from old-school Java-style to a concise Scala-style for such scenario of error handling: less verbosity, no ifs.

All the code you may find in a single place on GitHub.

--

--

Dmitry Komanov

Software developer, moved to Israel from Russia, trying to be aware of things.