Skip to content

Commit 638d37e

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 new scheme emits an error. We make `Show[T]` invariant in order to avoid ambiguous implicit errors 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 5e360d6 commit 638d37e

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
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: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotty
33
import scala.annotation.implicitNotFound
44

55
@implicitNotFound("No member of type class Show could be found for ${T}")
6-
trait Show[-T] {
6+
trait Show[T] {
77
def show(t: T): String
88
}
99

@@ -12,8 +12,8 @@ trait Show[-T] {
1212
* default instances in this object
1313
*/
1414
object Show {
15-
private[this] val defaultShow: Show[Any] = new Show[Any] {
16-
def show(x: Any) = x.toString
15+
private[this] def defaultShow[T]: Show[T] = new Show[T] {
16+
def show(x: T) = x.toString
1717
}
1818

1919
/** This class implements pimping of all types to provide a show method.
@@ -77,27 +77,15 @@ object Show {
7777
else "List(" + xs.map(_.show).mkString(", ") + ")"
7878
}
7979

80-
implicit val showNil: Show[Nil.type] = new Show[Nil.type] {
81-
def show(xs: Nil.type) = "List()"
82-
}
83-
8480
implicit def showOption[T](implicit st: Show[T]): Show[Option[T]] = new Show[Option[T]] {
8581
def show(ot: Option[T]): String = ot match {
8682
case Some(t) => "Some("+ st.show(t) + ")"
8783
case none => "None"
8884
}
8985
}
9086

91-
implicit val showNone: Show[None.type] = new Show[None.type] {
92-
def show(n: None.type) = "None"
93-
}
94-
9587
implicit def showMap[K,V](implicit sk: Show[K], sv: Show[V]): Show[Map[K,V]] = new Show[Map[K,V]] {
9688
def show(m: Map[K, V]) =
9789
"Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (", ") + ")"
9890
}
99-
100-
implicit def showMapOfNothing: Show[Map[Nothing,Nothing]] = new Show[Map[Nothing,Nothing]] {
101-
def show(m: Map[Nothing, Nothing]) = m.toString
102-
}
10391
}

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)