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}";
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 :)
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.
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 :)
Starting in Scala 2.10.0, Scala does support String Interpolation
Example:
val name = "James"
println(s"Hello, $name") // Hello, James
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.
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.
"1+1=\{1+1}!"
(13 characters) vs "1+1="+(1+1)+"!"
(16 characters) –
Gallantry ("1+1="+(1+1)+"!")
, because of operator precedence. –
Hollerman String interpolation is currently being added to Scala in the trunk, see this commit by Martin, so it will probably be available in Scala 2.10. See also the first test case for an example: https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/run/stringInterpolation.scala
Scala does have a similar feature for XML:
val x = doSomeCrazyCalculation
val xml = <foo>{x}</foo>
Consider that the XML literal is evaluated only once. See Scala multiline string placeholder for question variant dealing with multi-line strings.
© 2022 - 2024 — McMap. All rights reserved.
s"Hello, $str1"
(docs.scala-lang.org/overviews/core/string-interpolation.html) – Longueur