From 214258f54ebc7f8d9118aa3f84902d3b186742f7 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 30 Nov 2016 16:07:02 +0100 Subject: Add documentation to `Show` --- library/src/dotty/Show.scala | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/library/src/dotty/Show.scala b/library/src/dotty/Show.scala index 2febda0e7..21f80c7c5 100644 --- a/library/src/dotty/Show.scala +++ b/library/src/dotty/Show.scala @@ -7,20 +7,27 @@ 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 = new Show[Any] { def show(x: Any) = x.toString } + /** 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`. + */ implicit class ShowValue[V](val v: V) extends AnyVal { def show(implicit ev: Show[V] = defaultShow): String = ev.show(v) } implicit val stringShow = new Show[String] { - // From 2.12 spec: - // - // charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘) + // From 2.12 spec, `charEscapeSeq`: + // ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘) def show(str: String) = "\"" + { val sb = new StringBuilder -- cgit v1.2.3