Skip to content

Improve Expr.toString and Type.toString #16556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Dec 19, 2022

With this change, the toString can return the same as the show method.

We now capture the context in each Expr and Type instead of the Scope. This context can be used to compute the Scope.

Note that the context that is captured is the same one that is captured by the Quotes instance. As the expressions created with this Quotes should not escape the splice the created that Quotes, the Expr should not outlive the instance of Quotes. Therefore the captured context should not leak out of the macro expansion (or nested splice).

Behavior

  • This changes the default string representation of quoted expressions
def printExpr(e: Expr[T])(using Quotes) =
  println(e)
printExpr('{ f(x) })
// previous behavior
'{ ... }
// new behavior
'{ f(x) }
  • This also work well with pattern matching
'{ f(x) } match
  case '{ f(1) } => ...
  case '{ f(2) } => ...
// previous behavior
scala.MatchError: '{ ... }  (of class scala.quoted.Expr)
  at Foo.test(Foo$3:4)
  ...
// new behavior
MatchError: '{ f(x) } (of class scala.quoted.Expr)
  at Foo.test(Foo$3:4)
  ...
  • This would also simplify debuggers as they could directly show the string

@nicolasstucki

This comment was marked as outdated.

@dottybot

This comment was marked as outdated.

@dottybot

This comment was marked as outdated.

@nicolasstucki
Copy link
Contributor Author

test performance with #quotes please: 779a382

@dottybot
Copy link
Member

performance test scheduled for 779a382: 1 job(s) in queue, 0 running.

We now capture the context in each `Expr` and `Type` instead of the
`Scope`. This context can be used to compute the `Scope`.
@nicolasstucki
Copy link
Contributor Author

test performance with #quotes please

@dottybot
Copy link
Member

performance test scheduled: 1 job(s) in queue, 1 running.

1 similar comment
@dottybot
Copy link
Member

performance test scheduled: 1 job(s) in queue, 1 running.

@dottybot
Copy link
Member

Performance test finished successfully:

Visit https://dotty-bench.epfl.ch/16556/ to see the changes.

Benchmarks is based on merging with main (779a382)

1 similar comment
@dottybot
Copy link
Member

Performance test finished successfully:

Visit https://dotty-bench.epfl.ch/16556/ to see the changes.

Benchmarks is based on merging with main (779a382)

@nicolasstucki nicolasstucki marked this pull request as ready for review December 20, 2022 10:22
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What data is stored in a Context? In principle it looks cleaner to me to separate data types
like Expr and Type from the Context that prints them.

I am not seeing the motivation for the change. Can you describe what was the toString behavior before? And what is it now?

@odersky odersky assigned nicolasstucki and unassigned odersky Jan 4, 2023
@nicolasstucki nicolasstucki requested a review from odersky January 9, 2023 13:16
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still not sure this is the right way to do it. Normally, it's best to leave toString as the low-level debug info that simple shows the tree as is. I.e. not {...} but a case class term instead. And then there is a high-level show method that takes
a context and prints the tree in a high-level syntax. Why is that approach not applicable here?

@nicolasstucki
Copy link
Contributor Author

Replaced with #16663

nicolasstucki added a commit that referenced this pull request Jan 23, 2023
Alternative to #16556.

The default to string implementation gives more information on the
expression or type than `'{...}` or `Type.of[..]`. With this string
representation, we will know the class and instance hash.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants