Why is there no string interpolation in Scala?
Asked Answered
A

8

11

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}";
Actinometer answered 20/3, 2010 at 1:5 Comment(3)
This question is similar to #2184003 with much different answers.Moldboard
@Mitch: It really isn't the same question. The other asker wanted to know how to do it; I wanted to know the rationale behind the decision by the Scala designers, which is well-answered below.Actinometer
There is now (Scala 2.10 String Interpolation): s"Hello, $str1" (docs.scala-lang.org/overviews/core/string-interpolation.html)Longueur
G
16

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 :)

Grandee answered 20/3, 2010 at 8:33 Comment(1)
At first I thought the "Swiss keyboard" thing was a joke, but after reading the linked article I decided to pick this as best answer! Thanks for getting to the heart of my question which was "WHY??" is this feature missing from Scala.Actinometer
R
18

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.

Raggletaggle answered 25/2, 2011 at 23:16 Comment(0)
G
16

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 :)

Grandee answered 20/3, 2010 at 8:33 Comment(1)
At first I thought the "Swiss keyboard" thing was a joke, but after reading the linked article I decided to pick this as best answer! Thanks for getting to the heart of my question which was "WHY??" is this feature missing from Scala.Actinometer
L
8

Starting in Scala 2.10.0, Scala does support String Interpolation

Example:

val name = "James"
println(s"Hello, $name")  // Hello, James
Longueur answered 6/11, 2012 at 22:45 Comment(0)
M
7

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.

Mantle answered 20/3, 2010 at 1:35 Comment(0)
M
7

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.

Maureen answered 20/3, 2010 at 8:30 Comment(5)
Daniel (who gave the first answer here and is apparently too modest to promote it) has produced a solution, as well (dcsobral.blogspot.com/2010/01/…). Note that it requires a release later than Scala 2.8.0.Beta1.Manager
They do have a good point... it's just 4 chars versus 3. What would be nice is an Eclipse IDE modification to highlight "+...+" differently from normal start/end of string!Actinometer
I don't think that comparison is valid in the general case. If you need to interpolate an expression, the comparison should be: "1+1=\{1+1}!" (13 characters) vs "1+1="+(1+1)+"!" (16 characters)Gallantry
@BenLings: Or ("1+1="+(1+1)+"!"), because of operator precedence.Hollerman
String interpolation makes it easier to read, since the entire string is opened and closed once. It's not to save the number of characters that need to be typed.Mortonmortuary
M
6

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

Mudskipper answered 12/10, 2011 at 5:27 Comment(0)
P
1

Scala does have a similar feature for XML:

val x = doSomeCrazyCalculation
val xml = <foo>{x}</foo>
Phenylketonuria answered 20/3, 2010 at 5:38 Comment(0)
W
0

Consider that the XML literal is evaluated only once. See Scala multiline string placeholder for question variant dealing with multi-line strings.

Weathercock answered 3/5, 2011 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.