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}";
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}";
It was deemed that the extra compiler complexity that would result from it was not worth the gains to be had on one hand, and, on the other hand, that its absence would not be overly burdensome.
Scala does have a similar feature for XML:
val x = doSomeCrazyCalculation
val xml = <foo>{x}</foo>
I gather the feeling is that String interpolation is unnecessary, because it's not much more concise than concatenation:
val str2 = "Hello, ${str1}!"
val str2 = "Hello, "+str1+"!"
Arguing that the "+" operator on Strings should be formatted without a space, Martin Odersky says,
With the new convention, the need for string substitution (often put forward on Scala lists) all but disappears. Compare:
"test results: ${result1}, ${result2}"
with the first version above. You have saved one character per substitution, hardly worth a new syntax convention. In other words, Scala's string substitution syntax is written
"+...+"
instead of
${...}
or some other similar proposal. On the other hand, if you insist on spaces around the +, the story becomes much less convincing.
Incidentally, see the blog post, "String Interpolation in Scala" where it's emulated using reflection.
The proposed solution is to omit spaces when using +
.
"a="+a+", b="+b
It has been suggested that a Swiss keyboard layout makes this very natural to type, so the creators of Scala don't feel enough pain to justify complicating the langauge in this direction :)