-
Notifications
You must be signed in to change notification settings - Fork 1.1k
How to pass multiple type parameters to a function in a quote? #16967
Unanswered
pk044
asked this question in
Metaprogramming
-
I'm rewriting the following piece of Scala 2 code:
val Ps = extractFieldTypes(fields, T) val jsonFieldNames = extractJsonFieldNames(fields) val term = TermName(s"forProduct${fields.length}") val tree = q"""_root_.io.circe.Decoder.$term[$T, ..$Ps](..$jsonFieldNames)(${apply(T)})""" private def extractFieldTypes(fields: List[MethodSymbol], in: Type) = fields.map(resultType(_, in)) private def extractFieldNames(fields: List[MethodSymbol]) = fields.map(_.name.decodedName.toString) protected def extractJsonFieldNames(fields: List[MethodSymbol]): Seq[String] = extractFieldNames(fields)
to Scala 3 - and I'm curious how to pass the ps types to forProduct type paramaters:
private def _materializeDecoder[T](using Quotes)(using tp: Type[T])(tpe: quotes.reflect.Symbol, fields: List[quotes.reflect.Symbol]) = { import quotes.reflect.* val ps = fields.map(f => TypeRepr.of[T].memberType(f)) val jsonFieldNames = Expr(fields.map(_.name)) val tree = '{_root_.io.circe.Decoder.forProduct${fields.length}[T, ps]($jsonFieldNames)(${apply(T)})}} }
The solution above doesn't compile, seems like I can't use type splices either:
[error] 81 | val tree = '{_root_.io.circe.Decoder.forProduct${fields.length}[T, ${Expr(ps)}]($jsonFieldNames)(${apply(T)})}} [error] | ^^^^^^^^^^^ [error] | Type splicing with `$` in quotes not supported anymore [error] | [error] | Hint: To use a given Type[T] in a quote just write T directly
Is it possible to do that with quotes or do I have to give it another try by converting it into an AST?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment