Last active
August 29, 2015 14:27
-
-
Save mpilquist/f2ffd1c79733179a2ecf to your computer and use it in GitHub Desktop.
Simulacrum Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example of using Simulacrum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion := "2.11.7" | |
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0-M5" cross CrossVersion.full) | |
libraryDependencies += "com.github.mpilquist" %% "simulacrum" % "0.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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