diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-06 14:04:27 +0200 |
---|---|---|
committer | Dominik Gruntz <dominik.gruntz@fhnw.ch> | 2012-07-06 14:50:57 +0200 |
commit | 623b739bde7abce5271416fbd07e91dd547fd33f (patch) | |
tree | 76e439e3a0cf4530a96953cda68b7c302f8a12f1 /src/library | |
parent | c39c7276c38f9ef66fd7054609ef33627efe5177 (diff) | |
download | scala-623b739bde7abce5271416fbd07e91dd547fd33f.tar.gz scala-623b739bde7abce5271416fbd07e91dd547fd33f.tar.bz2 scala-623b739bde7abce5271416fbd07e91dd547fd33f.zip |
macro-based string interpolation formatter
This commit provides a macro based string interpolation formatter.
The macro is implemented in MacroImplementations.scala.
In order to still be able to build a new STARR, the implementation
in StringContext.f is not yet changed. This will be replaced in
a later commit.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/StringContext.scala | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index f400f18dab..ea58078d93 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -8,7 +8,7 @@ package scala -import collection.mutable.ArrayBuffer +import language.experimental.macros /** A class to support string interpolation. * This class supports string interpolation as outlined in Scala SIP-11. @@ -42,7 +42,7 @@ case class StringContext(parts: String*) { * @throws A `StringContext.InvalidEscapeException` if if a `parts` string contains a backslash (`\`) character * that does not start a valid escape sequence. */ - def s(args: Any*) = { + def s(args: Any*): String = { checkLengths(args: _*) val pi = parts.iterator val ai = args.iterator @@ -82,7 +82,7 @@ case class StringContext(parts: String*) { * string literally. This is achieved by replacing each such occurrence by the * format specifier `%%`. */ - def f(args: Any*) = { + def f(args: Any*): String = { checkLengths(args: _*) val pi = parts.iterator val bldr = new java.lang.StringBuilder @@ -92,8 +92,9 @@ case class StringContext(parts: String*) { var start = 0 var idx = 0 if (!first) { - if (strIsEmpty || (str charAt 0) != '%') + if (strIsEmpty || (str charAt 0) != '%') { bldr append "%s" + } idx = 1 } if (!strIsEmpty) { @@ -114,6 +115,10 @@ case class StringContext(parts: String*) { } bldr.toString format (args: _*) } + +// TODO The above method will be replaced by the following two lines as soon as the new STARR is available +// // The implementation is magically hardwired into `scala.tools.reflect.MacroImplementations.macro_StringInterpolation_f` +// def f(args: Any*): String = macro ??? } object StringContext { |