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

japgolly.scalajs.react.extra.internal.PxMacros.scala Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta6
Show newest version
package japgolly.scalajs.react.extra.internal

import scala.reflect.macros.blackbox.Context
import japgolly.scalajs.react.internal.MacroUtils

class PxMacros(val c: Context) extends MacroUtils {
  import c.universe._

  def extract[T: c.WeakTypeTag]: c.Expr[T] = {
    val T = weakTypeOf[T]

    val impl = T.dealias match {
      case TypeRef(_, sym, _) if sym.fullName startsWith "scala.Function" =>
        val argCount = sym.fullName.drop(14).toInt
        val names    = (0 until argCount).map(i => TermName(('a' + i).toChar.toString))
        val params   = names.map(n => ValDef(Modifiers(Flag.PARAM), n, TypeTree(), EmptyTree))
        q"""
          val px: _root_.japgolly.scalajs.react.extra.Px[$T] = ${c.prefix}
          ((..$params) => px.value()(..$names)): $T
        """

      case _ =>
        fail(s"Px.extract works with functions, not $T.")
    }

    c.Expr[T](impl)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy