jjmargon (8) [Avatar] Offline
#1
Hi.
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 (111) [Avatar] Offline
#2
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 (8) [Avatar] Offline
#3
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.