scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.79k stars 1.04k forks source link

Improve tuple string representation in reflection printers #21235

Open goshacodes opened 1 month ago

goshacodes commented 1 month ago

Compiler version

3.3.0

Output

I use Printer.TreeAnsiCode and Printer.TreeShortCode. and tuple

sVar *: dVar *: EmptyTuple

In generated code tuples looks something like this:

scala.Tuple$package.EmptyTuple.*:[dVar.type, scala.Tuple$package.EmptyTuple.type](dVar).*:[sVar.type, scala.*:[dVar, scala.Tuple$package.EmptyTuple]](sVar)

Which is very hard to read

Expectation

When should look like this

(sVar, dVar)
Gedochao commented 1 month ago

@goshacodes how exactly to reproduce the tuples from your generated code example?

goshacodes commented 1 month ago

I'll return with example today

goshacodes commented 1 month ago
object Example:
  inline def example: Unit =
    ${ ExampleMacro.example }

object ExampleMacro:
  def example(using quotes: Quotes): Expr[Unit] =
    import quotes.reflect.*
    val term = ValDef.let(
      Symbol.spliceOwner,
      List('{5}.asTerm, '{"foo"}.asTerm)
    ) { refs =>
      refs.foldRight[Term]('{ EmptyTuple }.asTerm) { (el, acc) =>
        Select.unique(acc, "*:")
          .appliedToTypes(List(el.tpe, acc.tpe))
          .appliedToArgs(List(el))
      }
    }
    '{println(${Expr(term.show(using Printer.TreeAnsiCode)})}

output:

{
  val x: scala.Int = 5
  val `x₂`: java.lang.String = "foo"
  scala.Tuple$package.EmptyTuple.*:[x.type, scala.Tuple$package.EmptyTuple.type](`x₂`).*:[x.type, scala.*:[x, scala.Tuple$package.EmptyTuple]](x)
}

expected:

(x, x2) or at least x *: x2 *: EmptyTuple