Skip to content

Instantly share code, notes, and snippets.

@szeiger
Created July 19, 2020 20:25
Show Gist options
  • Save szeiger/6682efbce480197042cbddd23764f847 to your computer and use it in GitHub Desktop.
Save szeiger/6682efbce480197042cbddd23764f847 to your computer and use it in GitHub Desktop.
import scala.quoted._
class Tag
class TableQuery[E](f: Tag => E) {
override def toString: String =
s"TableQuery(${f(new Tag)})"
}
object TableQuery {
def apply[E](f: Tag => E): TableQuery[E] = new TableQuery[E](f)
inline def apply[E]: TableQuery[E] = ${ applyExpr[E] }
def applyExpr[E](implicit qctx: QuoteContext, e: Type[E]): Expr[TableQuery[E]] = {
import qctx.tasty._
val eTpe = e.unseal.tpe
val tagTpe = summon[scala.quoted.Type[Tag]].unseal.tpe
val mt = MethodType(List("tag"))(_ => List(tagTpe), _ => eTpe)
val cons = Lambda(mt, { tag =>
Apply(
Select.unique(New(TypeIdent(eTpe.typeSymbol)), "<init>"),
List(tag.head.asInstanceOf[Term])
)
})
'{ TableQuery.apply[E](${ cons.seal.cast[Tag => E] }) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment