Skip to content

[REPL] Add show capability to common types #1761

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

Merged
merged 13 commits into from
Dec 14, 2016

Conversation

felixmulder
Copy link
Contributor

@felixmulder felixmulder commented Nov 30, 2016

This PR adds a trait Show[-T] to the Dotty library in order to print things in a better way for both the REPL and potentially other serializations. It also makes sure that the REPL shows all variables instead of toStringing them directly.

For now, there's a default show instance that calls toString so that there's a fallback in case of a Show instance not existing for a particular type. But potentially we could disallow printing things without a Show instance in the future...

Since we don't have control over the stdlib at the moment, we're stuck with providing default typeclass instances in the dotty.Show object. But ideally once @OlivierBlanvillain's HList implementation is in, perhaps we should investigate making a Show instance for DottyProduct or something similar so that we can have showing of all case classes 🎈

A known shortcoming in this PR is with regards to union types in e.g:

scala> List(1, 2, "three")
val res0: List[Int | String] = List(1, 2, three)
// instead of: List(1, 2, "three")

I tried to make the default instance down cast things if possible, but that put me into overflows for certain cases. Perhaps I could try harder if we really want this before bootstrapped library...

review: @odersky | @smarter

merging this solves: #1681

else "List(" + xs.map(_.show).mkString(", ") + ")"
}

implicit val showNil: Show[List[Nothing]] = new Show[List[Nothing]] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you try defining Show[Nil] and Show[None] instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did, but I didn't push it! (=

It actually gives a better representation of list:

scala> Nil
val res0: scala.collection.immutable.Nil.type = Nil
scala> List()
val res1: List[Nothing] = List()

instead of printing Nil for both.

@smarter
Copy link
Member

smarter commented Dec 1, 2016

See also the discussion in scala/scala#5222

@odersky
Copy link
Contributor

odersky commented Dec 14, 2016

LGTM 👍

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

Successfully merging this pull request may close these issues.

4 participants