jjmargon (11) [Avatar] Offline
Up to now, in this book, I've learned (among other things, of course) that when you break a business rule, from a functional perspective, is much better for function composition to wrap it in a "computation" object as Try.
Then, you can use Success or Failure to implement that Try.

In Listing 3.5, you break that way directly throwing a RuntimeException in the savingsAccount method when the rate of interest is <= 0

Why do you do it that way in that code?

Best regards.

P.S.: congrats for the book. For the time being, it's one of the best ones I've read for the last few years in its field.

Debasish Ghosh (116) [Avatar] Offline
Thanks for the question.

Note that I am using exceptions locally and it does not escape the function to the world. The function closeDateCheck returns a Try and the exceptions will be wrapped up within that Try. So the smart constructor never throws the exception - it always returns a computational structure Try.

In the later chapters I demonstrate other methods that uses other computational structures like Either or scalaz.\/ instead of exceptions.

Hope this helps.
jjmargon (11) [Avatar] Offline
OK. I am new also to Scala, so I misunderstood the code.

You're right. I didn't know that Scala internally, executes the apply method to the Try companion object in order wrap an exception and returns a Try anyway.

Thank you for your response.

Best regards.