Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use quotes to add a method with a dynamic name #20417

Unanswered
pwharned asked this question in Metaprogramming
Discussion options

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?

You must be logged in to vote

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /