diff options
author | Geoffrey Washburn <geoffrey.washburn@epfl.ch> | 2008-07-24 08:20:38 +0000 |
---|---|---|
committer | Geoffrey Washburn <geoffrey.washburn@epfl.ch> | 2008-07-24 08:20:38 +0000 |
commit | 3f9549bd6f4e2d4e2a67b2ba47d5bbbabe3434ef (patch) | |
tree | 96138d63c1cfa395dd1e35456851c385ae47417e /src/library | |
parent | 2e42f93bac81af7fcbb194c9f2722386c6aa3346 (diff) | |
download | scala-3f9549bd6f4e2d4e2a67b2ba47d5bbbabe3434ef.tar.gz scala-3f9549bd6f4e2d4e2a67b2ba47d5bbbabe3434ef.tar.bz2 scala-3f9549bd6f4e2d4e2a67b2ba47d5bbbabe3434ef.zip |
Changes/deprecation of Console.format.
Added RichString.format.
Started on better documentation in ModelAdditions.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Console.scala | 11 | ||||
-rw-r--r-- | src/library/scala/runtime/RichString.scala | 22 |
2 files changed, 28 insertions, 5 deletions
diff --git a/src/library/scala/Console.scala b/src/library/scala/Console.scala index 6136c25bc7..6e122f450e 100644 --- a/src/library/scala/Console.scala +++ b/src/library/scala/Console.scala @@ -175,16 +175,17 @@ object Console { * @param args the arguments used to instantiating the pattern. * @throws java.lang.IllegalArgumentException */ - def printf(text: String, args: Any*) { format(text, args: _*) } + def printf(text: String, args: Any*) { out.print(text format (args : _*)) } /** * @see <a href="#printf(java.lang.String,scala.Any*)" * target="contentFrame">Console.printf</a>. + * @deprecated For console output, use <code>Console.printf</code>. For <code>String</code> formatting, + * <code>RichString</code>'s <code>format</code> method. */ - def format(text: String, args: Any*): Unit = - if (text eq null) "null" else - out.printf(text, args.asInstanceOf[scala.runtime.BoxedObjectArray]. - unbox(args.getClass).asInstanceOf[Array[Object]] : _*) + @deprecated def format(text: String, args: Any*) { + if (text eq null) out.printf("null") else (out.print(text format (args : _*))) + } /** Read a full line from the terminal. Returns <code>null</code> if the end of the * input stream has been reached. diff --git a/src/library/scala/runtime/RichString.scala b/src/library/scala/runtime/RichString.scala index 412f2fd86c..ea4dd44e5f 100644 --- a/src/library/scala/runtime/RichString.scala +++ b/src/library/scala/runtime/RichString.scala @@ -220,6 +220,28 @@ final class RichString(val self: String) extends Proxy with RandomAccessSeq[Char self.getChars(0, length, result, 0) result } + + + /** <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 + * holes. Only works on Java 1.5 or higher! + * </p> + * <p> + * The interpretation of the formatting patterns is described in + * <a href="" target="contentFrame" class="java/util/Formatter"> + * <code>java.util.Formatter</code></a>. + * </p> + * + * @param args the arguments used to instantiating the pattern. + * @throws java.lang.IllegalArgumentException + */ + def format(args : Any*) : String = { + val m = classOf[String].getDeclaredMethod("format", classOf[String], classOf[Array[Object]]) + m.invoke(null, self, + args.asInstanceOf[scala.runtime.BoxedObjectArray]. + unbox(args.getClass).asInstanceOf[Array[Object]]).asInstanceOf[String] + } } object RichString { |