-
Notifications
You must be signed in to change notification settings - Fork 21
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
Comments
Imported From: https://issues.scala-lang.org/browse/SUGGEST-13?orig=1 |
Geoffrey Alan Washburn (washburn) said: Could you give a more detailed example of what you are looking for with regards to the first part of your message? |
yang said: 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. |
Geoffrey Alan Washburn (washburn) said: |
yang said: 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}] << 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. |
Andrew Foggin (andyfoggin) said: [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 ;-) |
@odersky said: |
Dave Ford (stokemasterjack) said: |
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:
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.
The text was updated successfully, but these errors were encountered: