Skip to content

Instantly share code, notes, and snippets.

@timjstewart
Created November 2, 2016 14:49
Show Gist options
  • Save timjstewart/74d0cbf3f7e82db759b1bb5ef3f10797 to your computer and use it in GitHub Desktop.
Save timjstewart/74d0cbf3f7e82db759b1bb5ef3f10797 to your computer and use it in GitHub Desktop.
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 _)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment