Skip to content

Fix ambiguous implicit for Show[Nothing] #3568

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 1 commit into from
Nov 28, 2017
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
6 changes: 6 additions & 0 deletions compiler/test-resources/repl/renderNothing
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scala> List()
val res0: List[Nothing] = List()
scala> Option.empty
val res1: Option[Nothing] = None
scala> Map()
val res2: Map[Nothing, Nothing] = Map()
37 changes: 6 additions & 31 deletions library/src/dotty/Show.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package dotty

import scala.annotation.implicitNotFound

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

/** Ideally show would only contain `defaultShow` and the pimped generic class,
* but since we can't change the current stdlib, we're stuck with providing
* default instances in this object
*/
object Show {
private[this] val defaultShow: Show[Any] = new Show[Any] {
def show(x: Any) = x.toString
trait LowPrioShow {
implicit def defaultShow[T]: Show[T] = new Show[T] {
def show(x: T) = x.toString
}
}

object Show extends LowPrioShow {
/** This class implements pimping of all types to provide a show method.
* Currently it is quite permissive, if there's no instance of `Show[T]` for
* any `T`, we default to `T#toString`.
Expand Down Expand Up @@ -46,18 +41,10 @@ object Show {
}
}

implicit val intShow: Show[Int] = new Show[Int] {
def show(i: Int) = i.toString
}

implicit val floatShow: Show[Float] = new Show[Float] {
def show(f: Float) = f + "f"
}

implicit val doubleShow: Show[Double] = new Show[Double] {
def show(d: Double) = d.toString
}

implicit val charShow: Show[Char] = new Show[Char] {
def show(c: Char) = "'" + (c match {
case '\b' => "\\b"
Expand All @@ -77,27 +64,15 @@ object Show {
else "List(" + xs.map(_.show).mkString(", ") + ")"
}

implicit val showNil: Show[Nil.type] = new Show[Nil.type] {
def show(xs: Nil.type) = "List()"
}

implicit def showOption[T](implicit st: Show[T]): Show[Option[T]] = new Show[Option[T]] {
def show(ot: Option[T]): String = ot match {
case Some(t) => "Some("+ st.show(t) + ")"
case none => "None"
}
}

implicit val showNone: Show[None.type] = new Show[None.type] {
def show(n: None.type) = "None"
}

implicit def showMap[K,V](implicit sk: Show[K], sv: Show[V]): Show[Map[K,V]] = new Show[Map[K,V]] {
def show(m: Map[K, V]) =
"Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (", ") + ")"
}

implicit def showMapOfNothing: Show[Map[Nothing,Nothing]] = new Show[Map[Nothing,Nothing]] {
def show(m: Map[Nothing, Nothing]) = m.toString
}
}
6 changes: 6 additions & 0 deletions library/test/dotty/ShowTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ class ShowTests {
assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show)
assertEquals("Map()", Map().show)
}

@Test def partialShow = {
case object Foo

assertEquals("Map(Foo -> \"Hello\")", Map(Foo -> "Hello").show)
}
}