Skip to content

Instantly share code, notes, and snippets.

Forked from mpilquist/.gitignore
Last active Aug 29, 2015
What would you like to do?
Simulacrum Example
Example of using Simulacrum
scalaVersion := "2.11.7"
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0-M5" cross CrossVersion.full)
libraryDependencies += "com.github.mpilquist" %% "simulacrum" % "0.4.0"
import simulacrum._
trait EmailMessage
trait EmailDestination
case class SendEmail(destination: EmailDestination, message: EmailMessage)
case class EmailSent(messageId: Int)
trait EmailService { def sendEmail(e: SendEmail): { def getMessageId: Int } }
@typeclass trait Emailable[A] {
def toEmailMessage(a: A): EmailMessage
object MyApp {
val ses: EmailService = ???
def sendEmail[T](destination: EmailDestination, message: T)(implicit emailable: Emailable[T]): EmailSent = {
val email = SendEmail(destination, emailable.toEmailMessage(message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment