Skip to content

Instantly share code, notes, and snippets.

@dozed
Created June 24, 2015 19:49
Show Gist options
  • Save dozed/ed5c3b2bc727ca752d68 to your computer and use it in GitHub Desktop.
Save dozed/ed5c3b2bc727ca752d68 to your computer and use it in GitHub Desktop.
import scala.language.experimental.macros
object implicit_dsls extends App {
trait DSLImpl {
def getImpl(route: String)(action: => Any): Unit
}
object MacroDSL {
import scala.reflect.macros.blackbox.Context
def getImplM(c: Context)(route: c.Expr[String])(action: c.Expr[Any]): c.Expr[Unit] = {
c.universe.reify {
println("macro get")
}
}
implicit val macroDSLImpl = new DSLImpl {
// macros cant implement abstract methods
//
// def getImpl(route: String)(action: => Any): Unit = macro getImplM
def getImpl(route: String)(action: => Any): Unit = {}
}
}
object DefaultDSL {
implicit val defaultDSLImpl = new DSLImpl {
def getImpl(route: String)(action: => Any): Unit = {
println("default get")
}
}
}
trait DSL
object DSL extends DSL {
def get(route: String)(action: => Any)(implicit dslImpl: DSLImpl) = dslImpl.getImpl(route)(action)
}
// import DefaultDSL._
import MacroDSL._
DSL.get("/foo") {
"ok"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment