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}";
Alex R
  • 11,364
  • 15
  • 100
  • 180
  • This question is similar to http://stackoverflow.com/questions/2183503/substitute-values-in-a-string-with-placeholders-in-scala/2189134 with much different answers. – Mitch Blevins Mar 21 '10 at 18:46
  • @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. – Alex R Mar 21 '10 at 23:39
  • 2
    There is now (Scala 2.10 String Interpolation): `s"Hello, $str1"` (http://docs.scala-lang.org/overviews/core/string-interpolation.html) – Eran Medan Nov 06 '12 at 22:41

8 Answers8

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.

Yang
  • 16,037
  • 15
  • 100
  • 142
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 :)

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
retronym
  • 54,768
  • 12
  • 155
  • 168
  • 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. – Alex R Mar 21 '10 at 23:42
8

Starting in Scala 2.10.0, Scala does support String Interpolation

Example:

val name = "James"
println(s"Hello, $name")  // Hello, James
Eran Medan
  • 44,555
  • 61
  • 184
  • 276
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.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
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.

Ben Lings
  • 28,823
  • 13
  • 72
  • 81
Matt R
  • 9,892
  • 10
  • 50
  • 83
  • 1
    Daniel (who gave the first answer here and is apparently too modest to promote it) has produced a solution, as well (http://dcsobral.blogspot.com/2010/01/string-interpolation-in-scala-with.html). Note that it requires a release later than Scala 2.8.0.Beta1. – Randall Schulz Mar 20 '10 at 14:32
  • 2
    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! – Alex R Mar 20 '10 at 17:28
  • 8
    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) – Ben Lings Mar 25 '10 at 17:36
  • 1
    @BenLings: Or `("1+1="+(1+1)+"!")`, because of operator precedence. – Mechanical snail Aug 14 '12 at 05:05
  • 1
    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. – Brent Faust Oct 27 '12 at 04:38
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

Arjan Blokzijl
  • 6,878
  • 2
  • 31
  • 27
1

Scala does have a similar feature for XML:

val x = doSomeCrazyCalculation
val xml = <foo>{x}</foo>
michaelg
  • 2,692
  • 1
  • 17
  • 16
0

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

Community
  • 1
  • 1
eptx
  • 801
  • 7
  • 17