From a190cfe4f0ece8221d8d7e4b27e4bf73ca665a56 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 30 Nov 2016 15:17:14 +0100 Subject: Get rid of nesting implicits --- library/src/dotty/Show.scala | 48 +++++++++++++++++++------------------- library/test/dotty/ShowTests.scala | 8 +------ 2 files changed, 25 insertions(+), 31 deletions(-) (limited to 'library') diff --git a/library/src/dotty/Show.scala b/library/src/dotty/Show.scala index 123fffdc7..103341d00 100644 --- a/library/src/dotty/Show.scala +++ b/library/src/dotty/Show.scala @@ -15,7 +15,9 @@ object Show { } implicit val stringShow = new Show[String] { - //charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘) + // From 2.12 spec: + // + // charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘) def show(str: String) = "\"" + str @@ -54,35 +56,33 @@ object Show { }) + "'" } - object List { - implicit def showList[T](implicit st: Show[T]) = new Show[List[T]] { - def show(xs: List[T]) = - if (xs.isEmpty) "Nil" - else "List(" + xs.map(_.show).mkString(", ") + ")" - } + implicit def showList[T](implicit st: Show[T]) = new Show[List[T]] { + def show(xs: List[T]) = + if (xs.isEmpty) "Nil" + else "List(" + xs.map(_.show).mkString(", ") + ")" + } - implicit val showNil = new Show[List[Nothing]] { - def show(xs: List[Nothing]) = "Nil" - } + implicit val showNil = new Show[List[Nothing]] { + def show(xs: List[Nothing]) = "Nil" } - object Option { - implicit def showOption[T](implicit st: Show[T]) = new Show[Option[T]] { - def show(ot: Option[T]): String = ot match { - case Some(t) => "Some("+ st.show(t) + ")" - case none => "None" - } + implicit def showOption[T](implicit st: Show[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 = new Show[Option[Nothing]] { - def show(n: Option[Nothing]) = "None" - } + implicit val showNone = new Show[Option[Nothing]] { + def show(n: Option[Nothing]) = "None" } - object Map { - implicit def showMap[K, V](implicit sk: Show[K], sv: Show[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 showMap[K, V](implicit sk: Show[K], sv: Show[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 = new Show[Map[Nothing, Nothing]] { + def show(m: Map[Nothing, Nothing]) = m.toString } } diff --git a/library/test/dotty/ShowTests.scala b/library/test/dotty/ShowTests.scala index 92539d0c4..7230106d5 100644 --- a/library/test/dotty/ShowTests.scala +++ b/library/test/dotty/ShowTests.scala @@ -34,7 +34,6 @@ class ShowTests { } @Test def showCar = { - import Show.List._ case class Car(model: String, manufacturer: String, year: Int) implicit val showCar = new Show[Car] { def show(c: Car) = @@ -51,14 +50,12 @@ class ShowTests { } @Test def showOptions = { - import Show.Option._ assertEquals("None", None.show) assertEquals("None", (None: Option[String]).show) assertEquals("Some(\"hello opt\")", Some("hello opt").show) } @Test def showMaps = { - import Show.Map._ val mp = scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2") assertEquals("Map(\"str1\" -> \"val1\", \"str2\" -> \"val2\")", mp.show) } @@ -67,9 +64,6 @@ class ShowTests { case class Car(model: String, manufacturer: String, year: Int) assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show) - assertEquals( - "Map(str1 -> val1, str2 -> val2)", - scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2").show - ) + assertEquals("Map()", Map().show) } } -- cgit v1.2.3