September 2018
Intermediate to advanced
398 pages
9h 43m
English
In the previous section, we changed the return type of nbOfMonthsSavings to return Option[Int]. The function returned None if the expenses arguments were greater than income. We are now going to change it to return an error message wrapped in Left.
We could use a simple string for our error message, but the best practice when using Either is to create an ADT for all of the possible error messages. Create a new Scala class in src/main/scala/retcalc called RetCalcError, as shown in the following code:
package retcalcsealed abstract class RetCalcError(val message: String)object RetCalcError { case class MoreExpensesThanIncome(income: Double, expenses: Double) extends RetCalcError( s"Expenses: $expenses >= $income. ...
Read now
Unlock full access