Skip to content

Commit 839fdfd

Browse files
committed
Fix ambiguous implicit for Show[Nothing]
PR #3421 changed implicit search: In the old scheme, a method with an implicit argument and a default value would give you the default value if called with an ambiguous implicit argument. The scheme emits an error. We now provide an implicit instance of `Show[Nothing]` in order to avoid ambiguous implicit error in the REPL for expressions like: ```scala scala> List() val res0: List[Nothing] = List() scala> Option.empty val res1: Option[Nothing] = None scala> Map() val res2: Map[Nothing, Nothing] = Map() ```
1 parent 463b996 commit 839fdfd

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
scala> List()
2+
val res0: List[Nothing] = List()
3+
scala> Option.empty
4+
val res1: Option[Nothing] = None
5+
scala> Map()
6+
val res2: Map[Nothing, Nothing] = Map()

library/src/dotty/Show.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ object Show {
7171
}) + "'"
7272
}
7373

74+
implicit val nothingShow: Show[Nothing] = new Show[Nothing] {
75+
def show(x: Nothing) = ???
76+
}
77+
7478
implicit def showList[T](implicit st: Show[T]): Show[List[T]] = new Show[List[T]] {
7579
def show(xs: List[T]) =
7680
if (xs.isEmpty) "List()"
@@ -96,8 +100,4 @@ object Show {
96100
def show(m: Map[K, V]) =
97101
"Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (", ") + ")"
98102
}
99-
100-
implicit def showMapOfNothing: Show[Map[Nothing,Nothing]] = new Show[Map[Nothing,Nothing]] {
101-
def show(m: Map[Nothing, Nothing]) = m.toString
102-
}
103103
}

library/test/dotty/ShowTests.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,5 @@ class ShowTests {
6464
case class Car(model: String, manufacturer: String, year: Int)
6565

6666
assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show)
67-
assertEquals("Map()", Map().show)
6867
}
6968
}

0 commit comments

Comments
 (0)