StringContext and macros: a simple example

自作多情 提交于 2019-12-02 23:52:17
som-snytt

This is the "song-and-dance" solution that handles interpolation of the timezone:

package object timezone {
  import scala.language.implicitConversions
  implicit def zoned(sc: StringContext) = new ZoneContext(sc)
}

package timezone {
  import scala.language.experimental.macros
  import scala.reflect.macros.Context
  import java.util.TimeZone

  class ZoneContext(sc: StringContext) {

    def tz(args: Any*): TimeZone = macro TimeZoned.tzImpl

    // invoked if runtime interpolation is required
    def tz0(args: Any*): TimeZone = {
      val s = sc.s(args: _*)
      val z = TimeZoned maybeTZ s getOrElse (throw new RuntimeException(s"Bad timezone $s"))
      TimeZone getTimeZone z
    }
  }
  object TimeZoned {
    def maybeTZ(s: String): Option[String] =
      if (TimeZone.getAvailableIDs contains s) Some(s) else None

    def tzImpl(c: Context)(args: c.Expr[Any]*): c.Expr[TimeZone] = {
      import c.universe._
      c.prefix.tree match {
        case Apply(_, List(Apply(_, List(tz @Literal(Constant(const: String)))))) =>
          maybeTZ(const) map (
            k => reify(TimeZone getTimeZone c.Expr[String](tz).splice)
          ) getOrElse c.abort(c.enclosingPosition, s"Bad timezone $const")
        case x =>
          val rts = x.tpe.declaration(newTermName("tz0"))
          val rt = treeBuild.mkAttributedSelect(x, rts)
          c.Expr[TimeZone](Apply(rt, args.map(_.tree).toList))
      }
    }
  }
}

Usage:

package tztest 

import timezone._

object Test extends App {

  val delta = 8
  //Console println tz"etc/GMT+$delta"  //java.lang.RuntimeException: Bad timezone etc/GMT+8
  Console println tz"Etc/GMT+$delta"
  Console println tz"US/Hawaii"
  //Console println tz"US/Nowayi"     //error: Bad timezone US/Nowayi
}

The problem is that you can't smuggle a compile-time instance of TimeZone into the code generated by your macro. You can, however, slip a string literal through, so you can generate code that will construct the TimeZone you want at run time, while still checking at compile time to make sure the identifier is available.

The following is a complete working example:

object TimeZoneLiterals {
  import java.util.TimeZone
  import scala.language.experimental.macros
  import scala.reflect.macros.Context

  implicit class TZContext(val sc: StringContext) extends AnyVal {
    def zone() = macro zoneImpl
  }

  def zoneImpl(c: reflect.macros.Context)() = {
    import c.universe._

    val tzExpr = c.prefix.tree match {
      case Apply(_, Apply(_, List(tz @ Literal(Constant(s: String)))) :: Nil)
        if TimeZone.getAvailableIDs contains s => c.Expr(tz)
      case _ => c.abort(c.enclosingPosition, "Invalid time zone!")
    }

    reify(TimeZone.getTimeZone(tzExpr.splice))
  }
}

The argument to reify will be the body of the generated method—literally, not after any kind of evaluation, except that the tzExpr.slice bit will be replaced by the compile-time string literal (if, of course, you've found it in the list of available identifiers—otherwise you get a compile-time error).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!