From 1f162e940c373007be2d24c36b6f6bfe49cb1486 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 25 Nov 2009 16:34:54 +0000 Subject: Added some logic to StringLike.format so that s... Added some logic to StringLike.format so that scala Numeric types can be used without a runtime error. --- .../scala/collection/immutable/StringLike.scala | 22 +++++++++++++++++----- src/library/scala/math/BigDecimal.scala | 3 ++- src/library/scala/math/BigInt.scala | 3 ++- src/library/scala/math/ScalaNumber.java | 1 + 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/library/scala/collection/immutable/StringLike.scala b/src/library/scala/collection/immutable/StringLike.scala index e993a412fc..91914b4e41 100644 --- a/src/library/scala/collection/immutable/StringLike.scala +++ b/src/library/scala/collection/immutable/StringLike.scala @@ -15,6 +15,7 @@ package immutable import generic._ import mutable.Builder import scala.util.matching.Regex +import scala.math.ScalaNumber /** * @since 2.8 @@ -220,6 +221,11 @@ self => } */ + private def unwrapArg(arg: Any): AnyRef = arg match { + case x: ScalaNumber => x.underlying + case x => x.asInstanceOf[AnyRef] + } + /**

* Uses the underlying string as a pattern (in a fashion similar to * printf in C), and uses the supplied arguments to fill in the @@ -228,14 +234,17 @@ self => *

* The interpretation of the formatting patterns is described in * - * java.util.Formatter. + * java.util.Formatter, with the addition that + * classes deriving from ScalaNumber (such as scala.BigInt and + * scala.BigDecimal) are unwrapped to pass a type which Formatter + * understands. *

* * @param args the arguments used to instantiating the pattern. * @throws java.lang.IllegalArgumentException */ - def format(args : Any*) : String = - java.lang.String.format(toString, args.asInstanceOf[scala.collection.Seq[AnyRef]]: _*) + def format(args : Any*): String = + java.lang.String.format(toString, args map unwrapArg: _*) /**

* Like format(args*) but takes an initial Locale parameter @@ -244,7 +253,10 @@ self => *

* The interpretation of the formatting patterns is described in * - * java.util.Formatter. + * java.util.Formatter, with the addition that + * classes deriving from ScalaNumber (such as scala.BigInt and + * scala.BigDecimal) are unwrapped to pass a type which Formatter + * understands. *

* * @param locale an instance of java.util.Locale @@ -252,6 +264,6 @@ self => * @throws java.lang.IllegalArgumentException */ def format(l: java.util.Locale, args: Any*): String = - java.lang.String.format(l, toString, args.asInstanceOf[scala.collection.Seq[AnyRef]]: _*) + java.lang.String.format(l, toString, args map unwrapArg: _*) } diff --git a/src/library/scala/math/BigDecimal.scala b/src/library/scala/math/BigDecimal.scala index d5a57b2c62..63fddc589b 100644 --- a/src/library/scala/math/BigDecimal.scala +++ b/src/library/scala/math/BigDecimal.scala @@ -178,7 +178,8 @@ extends ScalaNumber with ScalaNumericConversions case x => unifiedPrimitiveEquals(x) } - override protected def isWhole = (this remainder 1) == BigDecimal(0) + protected[math] def isWhole = (this remainder 1) == BigDecimal(0) + def underlying = bigDecimal /** Compares this BigDecimal with the specified BigDecimal for equality. */ diff --git a/src/library/scala/math/BigInt.scala b/src/library/scala/math/BigInt.scala index 5e4bb569b5..a27798adf8 100644 --- a/src/library/scala/math/BigInt.scala +++ b/src/library/scala/math/BigInt.scala @@ -127,7 +127,8 @@ class BigInt(val bigInteger: BigInteger) extends ScalaNumber with ScalaNumericCo case x => unifiedPrimitiveEquals(x) } - override protected def isWhole = true + protected[math] def isWhole = true + def underlying = bigInteger /** Compares this BigInt with the specified BigInt for equality. */ diff --git a/src/library/scala/math/ScalaNumber.java b/src/library/scala/math/ScalaNumber.java index 4ade1dee14..7d212426c4 100644 --- a/src/library/scala/math/ScalaNumber.java +++ b/src/library/scala/math/ScalaNumber.java @@ -18,4 +18,5 @@ package scala.math; */ public abstract class ScalaNumber extends java.lang.Number { protected abstract boolean isWhole(); + public abstract Object underlying(); } -- cgit v1.2.3