This is not just an idle quip... I wonder if anybody knows if there's an actual design reason why Scala does not support interpolation similar to Groovy and other "syntactically better Javas"?
e.g.
var str1 = "World"; var str2 = "Hello, ${str1}";
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.
Structure of an interpolated string. To identify a string literal as an interpolated string, prepend it with the $ symbol. You can't have any white space between the $ and the " that starts a string literal. To concatenate multiple interpolated strings, add the $ special character to each string literal.
Usage. Scala provides three string interpolation methods out of the box: s , f and raw .
According to the official Scala string interpolation documentation, when you precede your string with the letter s , you're creating a processed string literal. This example uses the “ s string interpolator,” which lets you embed variables inside a string, where they're replaced by their values.
Starting in Scala 2.10.0, Scala does support String Interpolation
Example:
val name = "James"
println(s"Hello, $name") // Hello, James
String interpolation is in Scala 2.10. See the SIP.
If you're using an earlier Scala version, there's also the ScalaEnhancedStrings compiler plugin.
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