2

我确信下面的下划线是单位,意思是函数返回值被忽略。(以下内容取自本书功能和反应域建模)。如果这是正确的,那么我对以下代码有疑问:

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>

这段代码能正常工作吗?它将,只要我们给 Function1 一些额外的权力,这是通过理解线程化的类型。

所以我应该将存储库传递给 res0 - 我不明白存储库是如何被限制到上面的每个贷记/借记方法的?我知道操作从存储库返回一个函数到帐户但是让我感到困惑的是所有这些下划线 - 下划线是否意味着我们摆脱了函数返回值的理解?如果我们忽略返回值,那么我们如何将这些函数传递给存储库?

4

1 回答 1

0

_并不意味着忽略返回值只是因为我们没有在代码中引用它。

于 2015-08-23T16:34:46.547 回答