Skip to content

Commit a190cfe

Browse files
committed
Get rid of nesting implicits
1 parent 8b7a7ea commit a190cfe

File tree

3 files changed

+26
-43
lines changed

3 files changed

+26
-43
lines changed

compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -704,18 +704,7 @@ class CompilingInterpreter(
704704
| if ($fullPath.asInstanceOf[AnyRef] != null) {
705705
| (if ($fullPath.toString().contains('\\n')) "\\n" else "") + {
706706
| import dotty.Show._
707-
| if ("$varType".matches(".*Map\\\\[.*,.*\\\\]")) {
708-
| import dotty.Show.Map._
709-
| $fullPath.show /*toString()*/ + "\\n"
710-
| } else if ("$varType".matches(".*List\\\\[.*\\\\]")) {
711-
| import dotty.Show.List._
712-
| $fullPath.show /*toString()*/ + "\\n"
713-
| } else if ("$varType".matches(".*Option\\\\[.*\\\\]")) {
714-
| import dotty.Show.Option._
715-
| $fullPath.show /*toString()*/ + "\\n"
716-
| } else {
717-
| $fullPath.show /*toString()*/ + "\\n"
718-
| }
707+
| $fullPath.show /*toString()*/ + "\\n"
719708
| }
720709
| } else {
721710
| "null\\n"

library/src/dotty/Show.scala

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ object Show {
1515
}
1616

1717
implicit val stringShow = new Show[String] {
18-
//charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘)
18+
// From 2.12 spec:
19+
//
20+
// charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘)
1921
def show(str: String) =
2022
"\"" +
2123
str
@@ -54,35 +56,33 @@ object Show {
5456
}) + "'"
5557
}
5658

57-
object List {
58-
implicit def showList[T](implicit st: Show[T]) = new Show[List[T]] {
59-
def show(xs: List[T]) =
60-
if (xs.isEmpty) "Nil"
61-
else "List(" + xs.map(_.show).mkString(", ") + ")"
62-
}
59+
implicit def showList[T](implicit st: Show[T]) = new Show[List[T]] {
60+
def show(xs: List[T]) =
61+
if (xs.isEmpty) "Nil"
62+
else "List(" + xs.map(_.show).mkString(", ") + ")"
63+
}
6364

64-
implicit val showNil = new Show[List[Nothing]] {
65-
def show(xs: List[Nothing]) = "Nil"
66-
}
65+
implicit val showNil = new Show[List[Nothing]] {
66+
def show(xs: List[Nothing]) = "Nil"
6767
}
6868

69-
object Option {
70-
implicit def showOption[T](implicit st: Show[T]) = new Show[Option[T]] {
71-
def show(ot: Option[T]): String = ot match {
72-
case Some(t) => "Some("+ st.show(t) + ")"
73-
case none => "None"
74-
}
69+
implicit def showOption[T](implicit st: Show[T]) = new Show[Option[T]] {
70+
def show(ot: Option[T]): String = ot match {
71+
case Some(t) => "Some("+ st.show(t) + ")"
72+
case none => "None"
7573
}
74+
}
7675

77-
implicit val showNone = new Show[Option[Nothing]] {
78-
def show(n: Option[Nothing]) = "None"
79-
}
76+
implicit val showNone = new Show[Option[Nothing]] {
77+
def show(n: Option[Nothing]) = "None"
8078
}
8179

82-
object Map {
83-
implicit def showMap[K, V](implicit sk: Show[K], sv: Show[V]) = new Show[Map[K, V]] {
84-
def show(m: Map[K, V]) =
85-
"Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (", ") + ")"
86-
}
80+
implicit def showMap[K, V](implicit sk: Show[K], sv: Show[V]) = new Show[Map[K, V]] {
81+
def show(m: Map[K, V]) =
82+
"Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (", ") + ")"
83+
}
84+
85+
implicit def showMapOfNothing = new Show[Map[Nothing, Nothing]] {
86+
def show(m: Map[Nothing, Nothing]) = m.toString
8787
}
8888
}

library/test/dotty/ShowTests.scala

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class ShowTests {
3434
}
3535

3636
@Test def showCar = {
37-
import Show.List._
3837
case class Car(model: String, manufacturer: String, year: Int)
3938
implicit val showCar = new Show[Car] {
4039
def show(c: Car) =
@@ -51,14 +50,12 @@ class ShowTests {
5150
}
5251

5352
@Test def showOptions = {
54-
import Show.Option._
5553
assertEquals("None", None.show)
5654
assertEquals("None", (None: Option[String]).show)
5755
assertEquals("Some(\"hello opt\")", Some("hello opt").show)
5856
}
5957

6058
@Test def showMaps = {
61-
import Show.Map._
6259
val mp = scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2")
6360
assertEquals("Map(\"str1\" -> \"val1\", \"str2\" -> \"val2\")", mp.show)
6461
}
@@ -67,9 +64,6 @@ class ShowTests {
6764
case class Car(model: String, manufacturer: String, year: Int)
6865

6966
assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show)
70-
assertEquals(
71-
"Map(str1 -> val1, str2 -> val2)",
72-
scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2").show
73-
)
67+
assertEquals("Map()", Map().show)
7468
}
7569
}

0 commit comments

Comments
 (0)