diff --git a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index f269fef64211..14a3b4ad0312 100644 --- a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -683,9 +683,36 @@ class CompilingInterpreter( code.print(resultExtractors.mkString("")) } + private val ListReg = """^.*List\[(\w+)\]$""".r + private val MapReg = """^.*Map\[(\w+),[ ]*(\w+)\]$""".r + private val LitReg = """^.*\((.+)\)$""".r + private def resultExtractor(req: Request, varName: Name): String = { val prettyName = varName.decode - val varType = string2code(req.typeOf(varName)) + // FIXME: `varType` is prettified to abbreviate common types where + // appropriate, and to also prettify literal types + // + // This should be rewritten to use the actual types once we have a + // semantic representation available to the REPL + val varType = string2code(req.typeOf(varName)) match { + // Extract List's paremeter from full path + case ListReg(param) => s"List[$param]" + // Extract Map's paremeters from full path + case MapReg(k, v) => s"Map[$k, $v]" + // Extract literal type from literal type representation. Example: + // + // ``` + // scala> val x: 42 = 42 + // val x: Int(42) = 42 + // scala> val y: "hello" = "hello" + // val y: String("hello") = "hello" + // ``` + case LitReg(lit) => lit + // When the type is a singleton value like None, don't show `None$` + // instead show `None.type`. + case x if x.lastOption == Some('$') => x.init + ".type" + case x => x + } val fullPath = req.fullPath(varName) val varOrVal = statement match { @@ -695,8 +722,10 @@ class CompilingInterpreter( s""" + "$varOrVal $prettyName: $varType = " + { | if ($fullPath.asInstanceOf[AnyRef] != null) { - | (if ($fullPath.toString().contains('\\n')) "\\n" else "") + - | $fullPath.toString() + "\\n" + | (if ($fullPath.toString().contains('\\n')) "\\n" else "") + { + | import dotty.Show._ + | $fullPath.show /*toString()*/ + "\\n" + | } | } else { | "null\\n" | } diff --git a/library/src/dotty/Show.scala b/library/src/dotty/Show.scala new file mode 100644 index 000000000000..2feeb29efbcc --- /dev/null +++ b/library/src/dotty/Show.scala @@ -0,0 +1,102 @@ +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 +} + +/** 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: Show[Any] = 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: Show[String] = new Show[String] { + // From 2.12 spec, `charEscapeSeq`: + // ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘) + def show(str: String) = + "\"" + { + val sb = new StringBuilder + str.foreach { + case '\b' => sb.append("\\b") + case '\t' => sb.append("\\t") + case '\n' => sb.append("\\n") + case '\f' => sb.append("\\f") + case '\r' => sb.append("\\r") + case '\'' => sb.append("\\'") + case '\"' => sb.append("\\\"") + case c => sb.append(c) + } + sb.toString + } + "\"" + } + + implicit val intShow: Show[Int] = new Show[Int] { + def show(i: Int) = i.toString + } + + implicit val floatShow: Show[Float] = new Show[Float] { + def show(f: Float) = f + "f" + } + + implicit val doubleShow: Show[Double] = new Show[Double] { + def show(d: Double) = d.toString + } + + implicit val charShow: Show[Char] = 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 + }) + "'" + } + + implicit def showList[T](implicit st: Show[T]): Show[List[T]] = new Show[List[T]] { + def show(xs: List[T]) = + if (xs.isEmpty) "Nil" + else "List(" + xs.map(_.show).mkString(", ") + ")" + } + + implicit val showNil: Show[Nil.type] = new Show[Nil.type] { + def show(xs: Nil.type) = "Nil" + } + + implicit def showOption[T](implicit st: Show[T]): Show[Option[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: Show[None.type] = new Show[None.type] { + def show(n: None.type) = "None" + } + + implicit def showMap[K,V](implicit sk: Show[K], sv: Show[V]): Show[Map[K,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: Show[Map[Nothing,Nothing]] = 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 new file mode 100644 index 000000000000..7230106d5b6d --- /dev/null +++ b/library/test/dotty/ShowTests.scala @@ -0,0 +1,69 @@ +package dotty + +import org.junit.Test +import org.junit.Assert._ + +class ShowTests { + import Show._ + + @Test def showString = { + assertEquals("\"\\thello world!\"", "\thello world!".show) + assertEquals("\"\\nhello world!\"", "\nhello world!".show) + assertEquals("\"\\rhello world!\"", "\rhello world!".show) + assertEquals("\"\\b\\t\\n\\f\\r\\\'\\\"\"", "\b\t\n\f\r\'\"".show) + } + + @Test def showFloat = { + assertEquals("1.0f", 1.0f.show) + assertEquals("1.0f", 1.0F.show) + } + + @Test def showDouble = { + assertEquals("1.0", 1.0d.show) + assertEquals("1.0", 1.0.show) + } + + @Test def showChar = { + assertEquals("'\\b'", '\b'.show) + assertEquals("'\\t'", '\t'.show) + assertEquals("'\\n'", '\n'.show) + assertEquals("'\\f'", '\f'.show) + assertEquals("'\\r'", '\r'.show) + assertEquals("'\\''", '\''.show) + assertEquals("'\\\"'", '\"'.show) + } + + @Test def showCar = { + case class Car(model: String, manufacturer: String, year: Int) + implicit val showCar = new Show[Car] { + def show(c: Car) = + "Car(" + c.model.show + ", " + c.manufacturer.show + ", " + c.year.show + ")" + } + + case class Shop(xs: List[Car], name: String) + implicit val showShop = new Show[Shop] { + def show(sh: Shop) = + "Shop(" + sh.xs.show + ", " + sh.name.show + ")" + } + + assertEquals("Car(\"Mustang\", \"Ford\", 1967)", Car("Mustang", "Ford", 1967).show) + } + + @Test def showOptions = { + assertEquals("None", None.show) + assertEquals("None", (None: Option[String]).show) + assertEquals("Some(\"hello opt\")", Some("hello opt").show) + } + + @Test def showMaps = { + val mp = scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2") + assertEquals("Map(\"str1\" -> \"val1\", \"str2\" -> \"val2\")", mp.show) + } + + @Test def withoutShow = { + case class Car(model: String, manufacturer: String, year: Int) + + assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show) + assertEquals("Map()", Map().show) + } +} diff --git a/project/Build.scala b/project/Build.scala index 002bcfbab01a..ea7ce31af2ee 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -375,7 +375,8 @@ object DottyBuild extends Build { settings( libraryDependencies ++= Seq( "org.scala-lang" % "scala-reflect" % scalaVersion.value, - "org.scala-lang" % "scala-library" % scalaVersion.value + "org.scala-lang" % "scala-library" % scalaVersion.value, + "com.novocode" % "junit-interface" % "0.11" % "test" ) ). settings(publishing) diff --git a/tests/repl/import.check b/tests/repl/import.check index 3ed0fe46c325..d53906ddce17 100644 --- a/tests/repl/import.check +++ b/tests/repl/import.check @@ -7,5 +7,5 @@ val res0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22) scala> buf ++= List(1, 2, 3) val res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22, 1, 2, 3) scala> buf.toList -val res2: scala.collection.immutable.List[Int] = List(22, 1, 2, 3) +val res2: List[Int] = List(22, 1, 2, 3) scala> :quit