I wanted to ask if there is any type of string interpolation in Scala. I have made a search on the subject but 'till now I have found that there is no string interpolation. Is there plans to get implemented in next versions?
Thanks!
UPDATE
String interpolation is going to be in scala 2.10 which you can try out since scala 2.10.RC1 is out (20/10/2012). You can check out this SIP for scala 2.11 which states that interpolated strings in the pattern matcher will be valid syntax. With the new string interpolation you can do something like this:
val age = 28
val name = "Gerry"
s"My name is $name and I am $age years old"
res0: String = My name is Gerry and I am 28 years old
But try out the documentation on all the interpolators that are available at the moment. Note that you can define your own interpolators! Try this link for more info.
String Interpolation refers to substitution of defined variables or expressions in a given String with respected values. String Interpolation provides an easy way to process String literals. To apply this feature of Scala, we must follow few rules: String must be defined with starting character as s / f /raw.
Interpolations use this escape to splice in arguments, and it can also be used to escape itself as the sequence $$ to represent a literal $ character. Because of its special handling during the parse, the $ could be used to escape a " character to represent a literal " withing a string.
Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.
In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
It's not in the (released) scala library yet. But there is a SIP (Scala Improvement Process) for the addition of this feature:
http://docs.scala-lang.org/sips/pending/string-interpolation.html
You can do it C-style:
"Interpolate my %s here" format List(1,2,3)
//String = Interpolate my List(1, 2, 3) here
or
List(1,2,3) formatted "Interpolate my %s here"
You can use these on anything with a toString (i.e. anything)
case class Foo(n: Int)
Foo(42) formatted "Here is a %s !!!!"
//String = Here is a Foo(42) !!!!
although the former is more flexible in terms of enabling multiple interpolations in a single string (since it can take multiple arguments).
yes, there is string interpolation in current scala releases via compiler plugin see http://jrudolph.github.com/scala-enhanced-strings
I use the xml hack on scala 2.9
val age = 28
val name = "Gerry"
<a>My name is {name} and I am {age} years old</a>.text
res0: String = My name is Gerry and I am 28 years old
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With