From 6ca71c23152a0575db7de2e45bbbf2c812e411f6 Mon Sep 17 00:00:00 2001 From: extempore Date: Mon, 23 Aug 2010 14:26:38 +0000 Subject: [PATCH] Pretty print tuples in the repl. Closes #3655, no review. git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@22829 5e8d7ff9-d8ef-0310-90f0-a4852d11357a --- src/library/scala/runtime/ScalaRunTime.scala | 21 +++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index e840c0f1a..6c40a3ef1 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -240,10 +240,11 @@ object ScalaRunTime { // maxPrintString limit will kick in way before this val maxElements = 10000 - def isScalaClass(x: AnyRef) = { - val pkg = x.getClass.getPackage - (pkg != null) && (pkg.getName startsWith "scala.") - } + def isScalaClass(x: AnyRef) = + Option(x.getClass.getPackage) exists (_.getName startsWith "scala.") + + def isTuple(x: AnyRef) = + x.getClass.getName matches """^scala\.Tuple(\d+).*""" // When doing our own iteration is dangerous def useOwnToString(x: Any) = x match { @@ -264,11 +265,13 @@ object ScalaRunTime { // The recursively applied attempt to prettify Array printing def inner(arg: Any): String = arg match { - case null => "null" - case x if useOwnToString(x) => x.toString - case x: AnyRef if isArray(x) => WrappedArray make x map inner mkString ("Array(", ", ", ")") - case x: Traversable[_] => x take maxElements map inner mkString (x.stringPrefix + "(", ", ", ")") - case x => x toString + case null => "null" + case x if useOwnToString(x) => x.toString + case x: AnyRef if isArray(x) => WrappedArray make x map inner mkString ("Array(", ", ", ")") + case x: Traversable[_] => x take maxElements map inner mkString (x.stringPrefix + "(", ", ", ")") + case x: Product1[_] if isTuple(x) => "(" + inner(x._1) + ",)" // that special trailing comma + case x: Product if isTuple(x) => x.productIterator map inner mkString ("(", ",", ")") + case x => x toString } // The try/catch is defense against iterables which aren't actually designed