Skip to content

String literals and string interpolation #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scabug opened this issue Dec 24, 2007 · 9 comments
Closed

String literals and string interpolation #308

scabug opened this issue Dec 24, 2007 · 9 comments

Comments

@scabug
Copy link

scabug commented Dec 24, 2007

I would like this feature, and when I thought about it in the past I liked the following syntax:

   "2 + 2 = \{2+2}.  I want some pie."

Essentially, {} is a new kind of backslash escape in strings. This would help with many issues where you build strings, but would not help with the issue you mention regarding regexes and backslashes. (For those, the """ quotes seem good.)

Somehow I find the {} much easier to look at than the version you would write right now:

   "2 + 2 = "+(2+2)+"I want some pie."

You will notice, though, that I do not like it enough to have put together a patch even after two years of very serious Scala hacking.

@scabug
Copy link
Author

scabug commented Dec 24, 2007

Imported From: https://issues.scala-lang.org/browse/SUGGEST-13?orig=1
Reporter: yang

@scabug
Copy link
Author

scabug commented Jan 15, 2008

Geoffrey Alan Washburn (washburn) said:
The problem with regular expression quoting can be handled by using the triple quote form, """ ... """.

Could you give a more detailed example of what you are looking for with regards to the first part of your message?

@scabug
Copy link
Author

scabug commented Jan 26, 2008

yang said:
Yeah, I actually knew about - I recently threw together the following hack (you can hopefully infer what the missing general-purpose functions are):

packag templates
import commons.Control._
import commons.Io._
import scala.collection.mutable._
object Templates {
  object StackItem extends Enumeration {
    val O,E,Q = Value
  }
  import StackItem._
  def main(args: Array[String]) {
    var e,q = false
    val stack = new Stack[Value]
    stack push O
    val in = using (TextReader(System.in)) (_ read)
    using (TextWriter(System.out)) { out =>
      val xs = for (c <- in) yield {
        c match {
          case '`' =>
            if (stack.top != E) { stack push E; "\"\"\"}{" }
            else { stack pop; "}{\"\"\"" }
          case '~' =>
            if (stack.top != Q) { stack push Q; "<p>{\"\"\"" }
            else { stack pop; "\"\"\"}</p>.text" }
          case _   => "" + c
        }
      }
      assert (stack.top == O)
      out println (xs mkString "")
    }
  }
}

The only key here was that I chose to use characters ` and ~ that I would probably not use in either the Scala language or the output language(s). It's total rubbish for that reason, but a smarter parser could be made to accept configurable delimiters.

I initially needed this for a project where I was using Scala to generate Java code. Since then, I found it useful in a number of other contexts. For instance, just now I used it to script the psql to produce some datasets, followed by the scripting of gnuplot to plotting these datasets. In both cases, this I was simply producing output files to be piped into psql and gnuplot. But with some more flexible quoting mechanisms similar to this, along with some combinators to manage processes, could all but replace bash in a lot of situations.

@scabug
Copy link
Author

scabug commented Jan 28, 2008

Geoffrey Alan Washburn (washburn) said:
Could you give an example of how you would write code using this? I am just trying to figure out how this will wind up usually being more concise than just using the string concatenation operator...

@scabug
Copy link
Author

scabug commented Feb 3, 2008

yang said:
One benefit of string interpolation is that it can be implemented more efficiently than using string concatenation, and without the boilerplate of setting up any StringBuilders (or otherwise hoping that the JVM performs this translation).

Another, and the main benefit, is simply that it is a much-less-verbose way to generate strings!

(Before reading ahead, note again that I am not endorsing these exact quoting characters. Replace ` and ~ with anything you want, e.g. posix regex's s||| or bash's $$() or Perl's qq() or C#'s @"".)

println(~
import java.util.*;
`imports`
public static class `classname` {
  public static void main(String[] args) {
    if (args.length > `minCount`) {
      switch (`decodeExpr(~args[`minCount - 1`] << 1~)`) {
        `
          for ((name,contents) <- states) yield {
            ~case `name.toUpperCase`: {
              `contents`
              break;
            }~
          }
        `
      }
    }
  }
}
~)

The method using

println("""
import java.util.*;
""" + imports + """
public static class """ + classname + """ {
  public static void main(String[] args) {
    if (args.length > """ + minCount + """) {
      switch (""" + decodeExpr(<p>args[{minCount - 1}] &lt;&lt; 1</p>) + """) {
        """ + {
          for ((name,contents) <- states) yield {
            """case """ + name.toUpperCase + """: {
              """ + contents + """
              break;
            }""" +
          }
        } + """
      }
    }
  }
}
""")

The former can be more efficient by, e.g., generating code using Scala XML's interpolation facilities.

@scabug
Copy link
Author

scabug commented Mar 27, 2008

Andrew Foggin (andyfoggin) said:
What do people think about the solution I demonstrated here?

[http://www.nabble.com/-scala--Extending-Scala-tp15856708p15856708.html]

(Note that the resolution of this issue would be a specific application of the more general approach I describe. Note also that my proof-of-concept only works for ids and not full expressions - full expressions ideally requires a parser plug-in capability but I'm still working on demonstrating that :-)

(Wacky idea for today: use the extension mechanism to embed EBNF-like grammar and use that to implement parser extensions ;-)

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone postponed deleted

@scabug
Copy link
Author

scabug commented May 9, 2009

Dave Ford (stokemasterjack) said:
yes - definitely need string iterpolation

@scabug
Copy link
Author

scabug commented Jan 25, 2012

@dcsobral said:
This became SIP-11, and has now been added to trunk under -Xexperimental, with a few limitations compared to SIP's proposal. I'm not sure how's the lifecycle of suggestions supposed to be, but given that it became a SIP, shouldn't this be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant