The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Tomer Ben David (8) [Avatar] Offline
I was sure the below underscores are for return type of type unit meaning function return value is ignored. If that's correct then I have a question about the below code:

case class AccountRepository(no: String)

trait AccountService[Account, Amount, Balance] {
  def open(no: String, name: String, openingDate: Option[Date]): AccountRepository => Try[Account]
  def close(no: String, closeDate: Option[Date])AccountRepository => Try[Account]
  def debit(no: String, amount: Amount): AccountRepository => Try[Account]
  def credit(no: String, amount: Amount): AccountRepository => Try[Account]
  def balance(no: String): AccountRepository => Try[Balance]

object App extends AccountService {
  def op(no: String) = for {
    _ <- credit(no, BigDecimal(100)) // isn't underscore here mean we neglect the return value as its in most cases unit? this mixes me up.  How do we refer again to the return value if it goes into the underscore which is normally used for unit response.
    _ <- credit(no, BigDecimal(300))
    _ <- debit(no, BigDecimal(160))
    b <- balance(no)
  } yield b

scala> op("a-123")
res0: AccountRepository => scala.util.Try[Balance] = <function1>

Will this code work correctly? It will, provided we give some additional power to Function1, which is the type that gets threaded through the comprehension.

So I should be passing to res0 the repository - I don't get how is the repository being throttled to each credit/debit method above? I understand the op returns a function from repository to account however what mixes me up are all these underscores - doesn't underscore means in for comprehension we get rid of the function return value? and if we ignore the returned value then how can we pass those functions the repository? credit debit functions are returning a function AccountRepository => Try[Account] so it looks to me like i somehow need the return value from a sequence step and not to assign it to _ which as far as i know ignores it so that I can further on apply it with the correct accountRepository so that the computation can take place, so i don't understand why its said not to be needed it looks to me like the return value is needed but the example does not populate it.
Debasish Ghosh (116) [Avatar] Offline
When we specify an _ in a for comprehension it doesn't mean that the return type is changed to Unit. It means that I don't need the name explicitly in my code. The threading is done as usual through the Try monad. As an exercise write a for-comprehension using _ and see the de-sugared version that the compiler generates.

Let me know if you need more details.