All Downloads are FREE. Search and download functionalities are using the official Maven repository.

japgolly.microlibs.name_fn.NameMacros.scala Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
package japgolly.microlibs.name_fn

trait NameImplicits {
  implicit def materializeNameFromString(body: String): Name =
    macro NameMacros.name

  implicit def materializeNameFnFromString(body: String): NameFn[Any] =
    macro NameMacros.nameFn

  implicit def nameFnFromString[A](a: A)(implicit ev: A => Name): NameFn[Any] =
    NameFn const ev(a)
}

final class NameMacros(val c: scala.reflect.macros.blackbox.Context) {
  import c.universe.{Name => _, _}

  def pkg = q"_root_.japgolly.microlibs.name_fn"

  def name(body: c.Expr[String]): c.Expr[Name] =
    body match {
      case Expr(Literal(Constant(s: String))) =>
        c.Expr[Name](q"$pkg.Name.now($s)")
      case _ =>
        c.Expr[Name](q"$pkg.Name($body)")
    }

  def nameFn(body: c.Expr[String]): c.Expr[NameFn[Any]] =
    c.Expr[NameFn[Any]](q"$pkg.NameFn.const($body)")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy