私はDebasish Ghoshの新刊「Functional and Reactive Domain Modeling」を読んでいます。本当に楽しんでいます。ここでは、Seq上でどのようなScala暗黙の変換が行われていますか?
Reporting.report(accts).foreach(println _)
のSeq [アカウント]を取るとのSeq [表示]に変換することができます:私は困惑している第5章で
ことの一つは、以下の行がどのようです。私は暗黙のうちにあることを知っているが、これをコンパイルするためにはどのようなステップが必要か?これはより一般的な暗黙のルールの特定のインスタンスですか? がのように見えますが、コンパイラはShowの特性をAccountオブジェクトに混ぜています。ありがとう!
ページから適応164:
import scala.util.Try
trait Show[T] {
def shows(t: T): Try[String]
}
trait ShowProtocol {
implicit val showAccount: Show[Account]
implicit val showCustomer: Show[Customer]
}
trait DomainShowProtocol extends ShowProtocol {
override implicit val showAccount: Show[Account] = (t: Account) => Try("Account")
override implicit val showCustomer: Show[Customer] = (t: Customer) => Try("Customer")
}
case class Account()
case class Customer()
object Reporting {
def report[T: Show](as: Seq[T]): Seq[Try[String]] = as.map(implicitly[Show[T]].shows _)
}
object DomainShowProtocol extends DomainShowProtocol
object Main {
def main(args: Array[String]): Unit = {
import DomainShowProtocol._
val accts: Seq[Account] = Seq(
Account(),
Account(),
Account()
)
Reporting.report(accts).foreach(println _)
}
}
感謝。彼らは本当に私が文法的な甘えを味わうのを助けてくれました。あなたが証言した注釈は全く新しいものでしたので、 "Aha!"瞬間! –