« Return to Thread: Why Martin hates string interpolation (Was: Re: Formatting Questions Summary)

Why Martin hates string interpolation (Was: Re: Formatting Questions Summary)

by Jorge Ortiz-3 :: Rate this Message:

Reply to Author | View in Thread

So, today on the #scala IRC channel, the perennial question of how to do string interpolation in Scala came up.

The most recent student of Scala was informed that the feature as he expected it did not exist, but that "escaping from string and "+concatenating+" some "+variables+" had been declared just a character or two longer than the ${alternative}".

Now, I have a lot of respect for Martin's decisions about language design. It's not a blind respect. Over time I've learned that when I disagree with him, it often turns out that I was wrong. However, on the issue of string interpolation I've always found Martin's stance to be rather puzzling. It just -looks- ugly. And it's so hard to -type-!

And then it hit me. Maybe Swiss keyboards have much more convenient access to the "+ and +" key combinations needed to concatenate strings.

I did a little research, and indeed, they do:
http://en.wikipedia.org/wiki/Keyboard_layout#Swiss_German.2C_Swiss_French.2C_Swiss_Italian.2C_Liechtenstein.2C_Luxembourg

Typing "+ and +" are just Shift+2+1 and Shift+1+2 on the Swiss keyboard. Either can be completed with a quick flick of the left wrist. It's hard to imagine something more natural.

Compare to the situation on the standard US keyboard:
http://en.wikipedia.org/wiki/Keyboard_layout#United_States

Typing "+ and +" involves Shift+<comma (,)>+<equals (=)> and Shift+<equals>+<comma>. Not only does this require both hands to type naturally, but even then the last two keys involve a reaching pinky finger. (One the Swiss keyboard, the similarly-positioned keys would be Shift+<a-grave (à)>+<circumflex-diacritic (^)>.)

I don't know how other keyboards fare, but I doubt most of them are as blessed as the Swiss keyboard.

But there you go: once again, Martin was right. Scala really doesn't need better string interpolation. I just need a more-Swiss keyboard.

--j

On Mon, Mar 17, 2008 at 12:14 PM, martin odersky <martin.odersky@...> wrote:
Hi all,

I agree with the formatting consensus, except for spaces around string
+. I am the one who proposed no spaces around + in strings. Let me
quickly explain my reasons (btw, I got this originally from Enno
Runne, several years ago).

1. When laying out strings, you want to be visually close to the
result you are getting. Compare:

   "test results: "+result1+", "+result2

with

   "test results: " + result1 + ", " + result2

Now, quickly: tell me where the spaces go: Is there a space before or
after the comma that gets printed? In the first version, this is
obvious, because the only spaces that are given are in the strings
themselves. And superfluous or missing spaces are one of
the most frequent source of errors in output.

2. 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.

3. String + is not the same as numeric +, and this often leads to
errors. A classical trap is:

  println("the sum is "+ x + y)

With the new convention, you'd tend to write:

 println("the sum is "+x + y)

This gives you a visual clue that something is wrong. So you'd
normally correct to:

 println("the sum is "+(x + y))

without having to run the program.

For all three reasons, I think an exception to the rule: ``put spaces
around all operators'' is warranted. Or otherwise said: + is
problematic as an operator on strings. Let's try not to write it as
one.

Cheers

 -- Martin

 « Return to Thread: Why Martin hates string interpolation (Was: Re: Formatting Questions Summary)