Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indiciate a failure for a function with a void result

Tags:

scala

I have a function in scala which has no return-value (so unit). This function can sometimes fail (if the user provided parameters are not valid). If I were on java, I would simply throw an exception. But on scala (although the same thing is possible), it is suggested to not use exceptions.

I perfectly know how to use Option or Try, but they all only make sense if you have something valid to return.

For example, think of a (imaginary) addPrintJob(printJob: printJob): Unit command which adds a print job to a printer. The job definition could now be invalid and the user should be notified of this.

I see the following two alternatives:

  1. Use exceptions anyway
  2. Return something from the method (like a "print job identifier") and then return a Option/Either/Try of that type. But this means adding a return value just for the sake of error handling.

What are the best practices here?

like image 679
theomega Avatar asked Sep 07 '15 07:09

theomega


1 Answers

You are too deep into FP :-) You want to know whether the method is successful or not - return a Boolean!

like image 132
Jacob Eckel Avatar answered Sep 29 '22 00:09

Jacob Eckel