Skip to content

Instantly share code, notes, and snippets.

@filosganga
Forked from mpilquist/.gitignore
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filosganga/614505d3147354ef05cb to your computer and use it in GitHub Desktop.
Save filosganga/614505d3147354ef05cb to your computer and use it in GitHub Desktop.
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))
EmailSent(ses.sendEmail(email).getMessageId)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment