aboutsummaryrefslogtreecommitdiff
path: root/library/src/dotty/Show.scala
blob: 123fffdc71c611382023235bd107b2edd4f0d401 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package dotty

import scala.annotation.implicitNotFound

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

object Show {
  implicit class ShowValue[V](val v: V) extends AnyVal {
    def show(implicit ev: Show[V] = null): String =
      if (ev != null) ev.show(v)
      else v.toString
  }

  implicit val stringShow = new Show[String] {
    //charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘)
    def show(str: String) =
      "\"" +
      str
      .replaceAll("\b", "\\\\b")
      .replaceAll("\t", "\\\\t")
      .replaceAll("\n", "\\\\n")
      .replaceAll("\f", "\\\\f")
      .replaceAll("\r", "\\\\r")
      .replaceAll("\'", "\\\\'")
      .replaceAll("\"", "\\\\\"") +
      "\""
  }

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

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

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

  implicit val charShow = new Show[Char] {
    def show(c: Char) = "'" + (c match {
      case '\b' => "\\b"
      case '\t' => "\\t"
      case '\n' => "\\n"
      case '\f' => "\\f"
      case '\r' => "\\r"
      case '\'' => "\\'"
      case '\"' => "\\\""
      case c    => c
    }) + "'"
  }

  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 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 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 (", ") + ")"
    }
  }
}