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

io.travisbrown.abstracted.Converter.scala Maven / Gradle / Ivy

The newest version!
package io.travisbrown.abstracted

import scala.language.experimental.macros

/**
 * A conversion from a wrapped value to some other type.
 */
trait Converter[A, B] {
  def apply(a: Empty[A]): B
}

private[abstracted] final object Converter extends internal.MacrosCompat {
  implicit def materialize[A, B]: Converter[A, B] = macro materializeImpl[A, B]

  def materializeImpl[A, B](c: Context)(implicit
    A: c.WeakTypeTag[A]
  ): c.Expr[Converter[A, B]] = {
    import c.universe._

    val view = implicitViews(c)(A.tpe) match {
      case List(unique) => unique
      case _ =>
        c.abort(c.enclosingPosition, s"Unable to find appropriate view for $A")
    }

    c.Expr[Converter[A, B]](
      q"""
        new Converter[$A, ${ resultType(c)(view.tpe) }] {
          final def apply(e: Empty[$A]) = $view(e.a)
        }
      """
    )
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy