summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-11-25 16:34:54 +0000
committerPaul Phillips <paulp@improving.org>2009-11-25 16:34:54 +0000
commit1f162e940c373007be2d24c36b6f6bfe49cb1486 (patch)
tree5bc8a0e2c1d1a12c5ab999bc09757388be100521
parenta3aa801a5181da49ec12164d54782e9d61359dfd (diff)
downloadscala-1f162e940c373007be2d24c36b6f6bfe49cb1486.tar.gz
scala-1f162e940c373007be2d24c36b6f6bfe49cb1486.tar.bz2
scala-1f162e940c373007be2d24c36b6f6bfe49cb1486.zip
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.
-rw-r--r--src/library/scala/collection/immutable/StringLike.scala22
-rw-r--r--src/library/scala/math/BigDecimal.scala3
-rw-r--r--src/library/scala/math/BigInt.scala3
-rw-r--r--src/library/scala/math/ScalaNumber.java1
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]
+ }
+
/** <p>
* 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 =>
* <p>
* The interpretation of the formatting patterns is described in
* <a href="" target="contentFrame" class="java/util/Formatter">
- * <code>java.util.Formatter</code></a>.
+ * <code>java.util.Formatter</code></a>, with the addition that
+ * classes deriving from ScalaNumber (such as scala.BigInt and
+ * scala.BigDecimal) are unwrapped to pass a type which Formatter
+ * understands.
* </p>
*
* @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: _*)
/** <p>
* Like format(args*) but takes an initial Locale parameter
@@ -244,7 +253,10 @@ self =>
* <p>
* The interpretation of the formatting patterns is described in
* <a href="" target="contentFrame" class="java/util/Formatter">
- * <code>java.util.Formatter</code></a>.
+ * <code>java.util.Formatter</code></a>, with the addition that
+ * classes deriving from ScalaNumber (such as scala.BigInt and
+ * scala.BigDecimal) are unwrapped to pass a type which Formatter
+ * understands.
* </p>
*
* @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();
}