Skip to content

Conversation

@lihaoyi
Copy link
Contributor

@lihaoyi lihaoyi commented Dec 29, 2025

  • Rather than collection.mutable.Buffer() echoing b: scala.collection.mutable.Buffer[T], it echoes collection.mutable.Buffer by default since scala.collection is imported bydefault
  • If you import scala.collection.mutable, it then echoes b: mutable.Buffer[T]
  • If you import scala.collection.mutable.Buffer, it echoes b: Buffer[T]

This feature was in Ammonite/PPrint, and while the implementation in the Scala compiler is totally different, in the end it does basically the same thing: when stringifying the type, it looks for things in scope that match the current type prefix, and if finds something then it prints the name of the name in scope rather than the full prefix

Added a unit test file to cover this, and updated all the existing tests that needed updating

val xs: List[Int] = List(1)
scala> scala.util.Try(1)
val res0: scala.util.Try[Int] = Success(1)
val res0: util.Try[Int] = Success(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we maybe show the full path when no import was added? This comes from the defaults, but might be confusing, since we also have java.util

Copy link
Member

Choose a reason for hiding this comment

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

But Java.util and any other util package is likely not imported

Copy link
Contributor Author

@lihaoyi lihaoyi Dec 29, 2025

Choose a reason for hiding this comment

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

I write code like util.Success and collection.Seq pretty frequently, taking advantage of the default imports. These shorthands are based on what symbols actually match, so there shouldn't be any ambiguity in meaning. If someone does import java.util or something it will fall back to the fully qualified name since the imported java.util will not be the same as scala.util

Copy link
Contributor

Choose a reason for hiding this comment

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

It's likely my personal preference then 😅 Maybe not important

Copy link
Contributor

Choose a reason for hiding this comment

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

I often wonder if util is dotc.util. Naming is so hard. If only superscripts could help here? Color-coding? Red for scala, blue for java, green for dotty.

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Overall, looks like a nice improvement. LGTM! Anyone has any other comments?

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.

4 participants