Wednesday, October 12, 2011

String Interpolation on 2.10?

Happiness is made of small things...

Welcome to Scala version 2.10.0.r25815-b20111010230908 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_23).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val x = 1
x: Int = 1

scala> "\{x}"
res0: String = 1

Requires the -Xexperimental flag. This test case reveals even more interesting things:

scala> val x = 1.1
x: Double = 1.1

scala> println("We have a \{ x ;2.2f}% chance of success")
We have a 1,10% chance of success

Mind you, there's a lot of things that have been hidden behind -Xexperimental, some of them for a long time now, and some of them ended up canned.
Edit: The proposal for this extension can be found here.

8 comments:

  1. The "\{}" syntaxe is a bit suprising, the consensus seeming to be on "${}" - even Dart seems to have taken that option.

    Do you know if there is a rationnal for that choice ? (I do understand that you discovered that features by chance on twitter, but in the case you knew more...)

    Thanks for the information,

    ReplyDelete
  2. I can think of at least one reason: it doesn't break any existing code. The \{ escape is illegal right now, so it is free to to be used for this.

    ReplyDelete
  3. why does it say:
    is equivalent to:
    ... (expr).unformatted ...

    here unformatted is a new method in class StringAdd, defined as follows:

    def show: String = self.toString

    is it show or unformatted or some scala magic that turns one into the other?

    ReplyDelete
  4. I think I miss the point.

    The SIP seems to focus on sparing key strokes: is it really the objective? Particularly, it does not mention the anonymous function literals capabilities shown in Dev Stonez comment. Is it implicit? Having this feature to participate in the blending of oop and fp sounds also cool to me: we could then have extractors based on those strings to make them bidirectional, like light regexp... I can see usage in log formatters, REST enpoints, etc.

    Or I still miss the point?

    ReplyDelete
  5. A backslash? Really? It should be dropped in favor of $ or #. When my eyes see \ it reads "escape". Why do the scala guys make things confusing for nothing so often... ~sigh~

    ReplyDelete
  6. Odersky's answer to string templates has always been to write "a"+b+"c". There's even a anecdote about this being reasonable for him because on swiss keyboard the double quote and the plus sign are unshifted and/or on different fingers, which seems to be true.

    So, to him, it is just a matter of sparing key strokes. But lots of people keep asking for string template, so...

    To me, it is *not* a matter of sparing keystrokes. Double quotes are not open/close signs, so it is difficult to tell apart what is string and what is not when using his proposed style. Besides, I don't use a swiss keyboard. I *much* prefer interpolation, even with his proposed syntax.

    Furthermore, when using "+" to interpolate, I can't put expressions without further adding parenthesis. In this proposal, I can.

    Now, one problem with his proposal is that string literals using triple-double-quotes are not in, and they are really useful to insert mildly long texts in the code. For big programs, that's not good style, but it is very useful for scripting. So, we do not gain everything.

    And as for \{ instead of ${ and #{, there's one huge advantage: it doesn't break a SINGLE line of existing code. Why ${ or #{? Just because "everyone" uses that? Well, screw everyone. The concessions Scala made to be "more like Java" crippled the language in many ways, so I'm not particularly fond of being "like" something else (Java doesn't have interpolation) unless it brings something USEFUL to the table.

    ReplyDelete
  7. Daniel, I can't agree more
    string interpolation is a must for any modern language
    your last comment makes it extremely clear

    the only things which makes me wonder is how would this solution compare to scala-enhanced-strings

    ReplyDelete