Created
July 19, 2020 20:25
-
-
Save szeiger/6682efbce480197042cbddd23764f847 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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