PROPOSAL: Multiline strings (original) (raw)

Jeremy Manson jeremy.manson at gmail.com
Sun Mar 1 00:58:50 PST 2009


One thought springs to mind: the indentation will be weird in cases where white space matters: { StringBuilder sb = new StringBuilder(); sb.append("""in this case we need multiple lines but we can't start them with lots of whitespace"""); // ... System.err.println(sb); }

Alternatively, you could have a special syntax where 4 quotes strips out leading whitespace on each line:

{ StringBuilder sb = new StringBuilder(); sb.append(""""select a from Area a, CountryCodes cc where cc.isoCode='UA' and a.owner = cc.country """"); }

Or you could use the C++ style? { StringBuilder sb = new StringBuilder(); sb.append("select a from Area a, CountryCodes cc" "where" "cc.isoCode='UA'" "and" "a.owner = cc.country"); }

Frankly, to me, the big win would actually not be multiline literals, but would be escaped String literals. I'm sick of writing all of my regexps with twice as many \ characters as they need.

Jeremy

On Sat, Feb 28, 2009 at 7:46 PM, <rssh at gradsoft.com.ua> wrote:

AUTHOR(s): Ruslan Shevchenko

OVERVIEW:  FEATURE SUMMARY: add multiline strings to java language.  MAJOR ADVANTAGE: Possibility more elegant to write code part of codes in other languages,  such as sql constructions or rendering of html components.  MAJOR DISADVANTAGE I don't know  ALTERNATIVES:  use String "+=" operator.  using groovy instead java in utility classes. EXAMPLES SIMPLE EXAMPLE:  StringBuilder sb = new StringBuilder();  sb.append("""select a from Area a, CountryCodes cc  where cc.isoCode='UA'  and a.owner = cc.country  """);  if (question.getAreaName()!=null) { sb.append("""and  a.name like ? """); sqlParams.setString(++i,question.getAreaName());  }  instead:  StringBuilder sb = new StringBuilder();  sb.append("select a from Area a, CountryCodes cc\n");  sb.append("where cc.isoCode='UA'\n");  sb.append("and a.owner=cc.country'\n");  if (question.getAreaName()!=null) { sb.append("and a.name like ?"); sqlParams.setString(++i,question.getAreaName());  }

DETAILS:  Multiline strings are part of program text, which begin and ends  by three double quotes. (as in groovy and scala) Text withing such  brackets processed as multiline string with all rulles as normal Java  string literals, except it can be multiline. After parsing multiline  string is concatenation of lines with inserted value of system property 'line.separator' between thems. COMPILATION:  Multiline strings created and used in .class files exactly as ordinary strings. TESTING:  Nothing special. add multiline strings to test-cases. LIBRARY SUPPORT:  None.  (May be exists sence add simple template processing to standard library, but  I think this is goal of next janguage iteration. Now exists many good external  frameworks, such as velocity: better wait and standartize support of winner) REFLECTIVE APIS: None OTHER CHANGES: None MIGRATION: None COMPABILITY  None REFERENCES  http://bugs.sun.com/viewbug.do?bugid=4165111



More information about the coin-dev mailing list