-
Couldn't load subscription status.
- Fork 1.1k
How to use quotes to add a method with a dynamic name #20417
-
I am trying to return a new class inline defined methods. This is easy enough with quotes.
object StatementGenerator {
// Auxilirary function to create the class as the inlined main maro function needs a single expression.
private def insertCallImplementation[A <: Tuple: Type](
tableName: Expr[String],
columnMapping: Expr[A]
)(using
Quotes
): Expr[PreparedStatement[CallArgs[A]]] =
new StatementGenerator()
.insertPreparedStatementImpl[A](tableName, columnMapping)
inline def insertPreparedStatement[A <: Tuple](inline tableName: String)(
inline columnMapping: A
): PreparedStatement[CallArgs[A]] = ${
insertCallImplementation[A]('tableName, 'columnMapping)
}
@experimental
private def selectCallImplementation[A <: Tuple : Type, B<: Tuple: Type](
tableName: Expr[String],
columnMapping: Expr[A],
filterMapping: Expr[B]
)(using
Quotes
): Expr[PreparedStatementFiltered[CallArgs[A],CallArgs[B]]] =
new StatementGenerator()
.selectPreparedStatementImpl[A,B](tableName, columnMapping, filterMapping)
@experimental
inline def selectPreparedStatement[A <: Tuple, B<:Tuple](inline tableName: String)(
inline columnMapping: A)(inline filterMapping: B
): PreparedStatementFiltered[CallArgs[A], CallArgs[B]] = ${
selectCallImplementation[A, B]('tableName, 'columnMapping, 'filterMapping)
}
}
val cl = '{
new PreparedStatementFiltered[CallArgs[A], CallArgs[B]](UnsafeStatementFiltered($sql)) {
def go: String = "hello"
override def retrieve(rs: java.sql.ResultSet): Tuple = ${ combinedRsGet }.apply(rs)
override def retrieveJson(rs: java.sql.ResultSet): String = ${ combinedRsJsonGet }.apply(rs)
}
}.asExprOf[PreparedStatementFiltered[CallArgs[A], CallArgs[B]]]
However, i want to be able to rename the methods based on a string that is evaluated at compile time.
I was able to figure out how to do this with the Expr API after three days but my code became so wacked out it refused to compile for some other reasons.
What I would like is help either
A: Producing this structure with the Expr API so I can add a method with a new name or
B: Is it posssible with quotes api to do this?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Take a look at this discussion, it may be of help:
https://users.scala-lang.org/t/scala-3-macro-create-instance-of-a-type/8016/4
https://stackoverflow.com/questions/68550985/method-override-with-scala-3-macros
Beta Was this translation helpful? Give feedback.