Skip to content

Fix #4301: Show null as "null" #4310

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 4 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/test-resources/repl/i4301
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
scala> List(null)
val res0: List[Null] = List(null)
scala> List[String](null)
val res1: List[String] = List(null)
3 changes: 1 addition & 2 deletions library/src/dotty/Show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ object Show extends LowPrioShow {
* any `T`, we default to `T#toString`.
*/
implicit class ShowValue[V](val v: V) extends AnyVal {
def show(implicit ev: Show[V]): String =
ev.show(v)
def show(implicit ev: Show[V]): String = if (v == null) "null" else ev.show(v)
}

/** Adds escaping backslashes in a string so that it could be put in source code.
Expand Down
16 changes: 14 additions & 2 deletions library/test/dotty/ShowTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class ShowTests {

@Test def showOptions = {
assertEquals("None", None.show)
val empty = Option.empty
assertEquals("None", empty.show)
assertEquals("None", (None: Option[String]).show)
assertEquals("Some(\"hello opt\")", Some("hello opt").show)
}
Expand All @@ -65,8 +67,6 @@ class ShowTests {
@Test def withoutShow = {
case class Car(model: String, manufacturer: String, year: Int)
assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show)
assertEquals("Map()", Map[Nothing, Nothing]().show)
assertEquals("List()", List().show)
}

@Test def partialShow = {
Expand All @@ -79,4 +79,16 @@ class ShowTests {
assertEquals("Array(1)", Array(1).show)
assertEquals("Array(1, 2, 3)", Array(1, 2, 3).show)
}

@Test def showNull = {
assertEquals("null", (null: String).show)
assertEquals("List(null)", List(null).show)
assertEquals("List(null)", List[String](null).show)
}

@Test def showNothing = {
val emptyMap = Map()
assertEquals("Map()", emptyMap.show)
assertEquals("List()", List().show)
}
}