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 pass multiple type parameters to a function in a quote? #16967

Unanswered
pk044 asked this question in Metaprogramming
Discussion options

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!

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

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