From 46d5e73c11bccd0e892429e4c3f2ac1a02cea2a9 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 12 Oct 2010 22:16:51 +0000 Subject: Two improvements (or so I claim) to MatchErrors. toString when constructing the exception: it need not be called until/unless it is printed, which it may never be. 2) Include the name of the class which triggered it. Don't tell me you haven't wanted that as many times as I have. (Sidebar on commit message semantics: I assume nobody interprets "no review" to mean anything like "unreviewable decision!" It only means I'm pretty sure the code does what I intended. The floor is always open.) That said, no review. --- src/library/scala/MatchError.scala | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/library/scala/MatchError.scala b/src/library/scala/MatchError.scala index 31783da4d3..5f0f7e2dfc 100644 --- a/src/library/scala/MatchError.scala +++ b/src/library/scala/MatchError.scala @@ -19,7 +19,13 @@ package scala * @version 1.1, 05/03/2004 * @since 2.0 */ -final class MatchError(msg: String) extends RuntimeException(msg) { - def this(obj: Any) = - this(if (null != obj) obj.toString() else "null") +final class MatchError(obj: Any) extends RuntimeException { + /** There's no reason we need to call toString eagerly, + * so defer it until getMessage is called. + */ + private lazy val objString = + if (obj == null) "null" + else obj.toString() + " (of class " + obj.asInstanceOf[AnyRef].getClass.getName + ")" + + override def getMessage() = objString } -- cgit v1.2.3