11package dotty
22
3- import scala .annotation .implicitNotFound
4-
5- @ implicitNotFound(" No member of type class Show could be found for ${T}" )
6- trait Show [- T ] {
3+ trait Show [T ] {
74 def show (t : T ): String
85}
96
10- /** Ideally show would only contain `defaultShow` and the pimped generic class,
11- * but since we can't change the current stdlib, we're stuck with providing
12- * default instances in this object
13- */
14- object Show {
15- private [this ] val defaultShow : Show [Any ] = new Show [Any ] {
16- def show (x : Any ) = x.toString
7+ trait LowPrioShow {
8+ implicit def defaultShow [T ]: Show [T ] = new Show [T ] {
9+ def show (x : T ) = x.toString
1710 }
11+ }
1812
13+ object Show extends LowPrioShow {
1914 /** This class implements pimping of all types to provide a show method.
2015 * Currently it is quite permissive, if there's no instance of `Show[T]` for
2116 * any `T`, we default to `T#toString`.
@@ -46,18 +41,10 @@ object Show {
4641 }
4742 }
4843
49- implicit val intShow : Show [Int ] = new Show [Int ] {
50- def show (i : Int ) = i.toString
51- }
52-
5344 implicit val floatShow : Show [Float ] = new Show [Float ] {
5445 def show (f : Float ) = f + " f"
5546 }
5647
57- implicit val doubleShow : Show [Double ] = new Show [Double ] {
58- def show (d : Double ) = d.toString
59- }
60-
6148 implicit val charShow : Show [Char ] = new Show [Char ] {
6249 def show (c : Char ) = " '" + (c match {
6350 case '\b ' => " \\ b"
@@ -77,27 +64,15 @@ object Show {
7764 else " List(" + xs.map(_.show).mkString(" , " ) + " )"
7865 }
7966
80- implicit val showNil : Show [Nil .type ] = new Show [Nil .type ] {
81- def show (xs : Nil .type ) = " List()"
82- }
83-
8467 implicit def showOption [T ](implicit st : Show [T ]): Show [Option [T ]] = new Show [Option [T ]] {
8568 def show (ot : Option [T ]): String = ot match {
8669 case Some (t) => " Some(" + st.show(t) + " )"
8770 case none => " None"
8871 }
8972 }
9073
91- implicit val showNone : Show [None .type ] = new Show [None .type ] {
92- def show (n : None .type ) = " None"
93- }
94-
9574 implicit def showMap [K ,V ](implicit sk : Show [K ], sv : Show [V ]): Show [Map [K ,V ]] = new Show [Map [K ,V ]] {
9675 def show (m : Map [K , V ]) =
9776 " Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (" , " ) + " )"
9877 }
99-
100- implicit def showMapOfNothing : Show [Map [Nothing ,Nothing ]] = new Show [Map [Nothing ,Nothing ]] {
101- def show (m : Map [Nothing , Nothing ]) = m.toString
102- }
10378}
0 commit comments