我确定下面的下划线是单位含义函数返回值被忽略。 (以下内容摘自书籍功能和反应域建模)。如果这是正确的,那么我对以下代码有疑问:
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存储库 - 我不知道存储库是如何被限制到上面的每个信用/借记方法?我理解op返回一个从存储库到帐户的函数然而混合我的是所有这些下划线 - 不是下划线意味着理解我们摆脱了函数返回值?如果我们忽略返回的值,那么我们如何将这些函数传递给存储库?