Frank Zinner (2) [Avatar] Offline
#1
I think theres an "implicit" missing in Listing 1.5:

In the definition def debit(a: Account, amount: Amount): Try[Account] and def credit(a: Account, amount: Amount) = Success(...)

The return types are Try[Account] and Success() - in the REPL :

val a = Account(“a1”, “John”, today)
a.balance == Balance(0)
val b = credit(a, 1000) // this will work because "a" has type Account

but this:
val c = debit(b, 200) // won't, because credit(a, 1000) will return type Success[Account] and not Account.

I could fix this by defining an implicit that converts Success[Account] to Account otherwise the compiler can not find any conversion.
Debasish Ghosh (111) [Avatar] Offline
#2
Thanks for pointing it out. But adding the implicit conversion is not the recommended approach. The types need to be corrected.The following 2 things need to be done for complete correctness of return types and implementation:

1. annotate the return type for credit to Try[Account]
2. invoke the following debit call as val c = b.flatMap { debit(_, 200) } or use a for comprehension

In fact the code snippet following Listing 1.5 shows the correct usage of debit & credit.

I will correct it in the next MEAP.

Thanks.