tag:blogger.com,1999:blog-741750605858169835.post7666021414661827646..comments2024-01-24T14:53:02.919+00:00Comments on Stephen Colebourne's blog: Java 7 - Multi-line String literalsStephen Colebournehttp://www.blogger.com/profile/01454237967846880639noreply@blogger.comBlogger36125tag:blogger.com,1999:blog-741750605858169835.post-80708266850596180112018-01-28T23:35:29.861+00:002018-01-28T23:35:29.861+00:00Now being considered for Java: http://openjdk.java...Now being considered for Java: http://openjdk.java.net/jeps/8196004Stephen Colebournehttps://www.blogger.com/profile/01454237967846880639noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-7556769362121966282017-04-19T01:31:01.212+01:002017-04-19T01:31:01.212+01:00and still editing every damn line if you want to p...and still editing every damn line if you want to paste it into your db client to execute.ideasculptorhttps://www.blogger.com/profile/04137163282057146867noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-54297346152666921682017-04-07T19:19:47.849+01:002017-04-07T19:19:47.849+01:00Nine years later and still concatenating... :'...Nine years later and still concatenating... :'(Anonymoushttps://www.blogger.com/profile/11615330986089133736noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-8545514824325081142015-09-19T04:26:53.961+01:002015-09-19T04:26:53.961+01:00It is honestly pathetic that Java hasn't added...It is honestly pathetic that Java hasn't added this yetAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-37999960563075404292014-03-05T01:25:26.142+00:002014-03-05T01:25:26.142+00:00The lack of heredoc in Java has made many physioth...The lack of heredoc in Java has made many physiotherapists rich. Two things painfully missed after tasting PHP are multiline strings and associative arrays (yea maps). Anyways Groovying in the meantime.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-42178910968422305562013-11-03T06:39:57.035+00:002013-11-03T06:39:57.035+00:00I'm not sure how well it would perform in the ...I'm not sure how well it would perform in the real world, but what if multiline strings were handled similar to JavaDocs are?<br /><br />/*This is<br /> *a javadoc<br /> *with multiple lines,<br /> *right?<br />*/<br /><br />/+This is<br /> +a string<br /> +with multiple lines,<br /> +right?<br /> +/<br /><br />Looks like the syntax could use a little bit of tweaking to avoid dealing with extra whitespace and newlines, but that's the first thing that came to mind after throwing out everything else that either had poor syntax or ruined code structure. I kinda like it because it'd use the "+" character just like if you were concatenating a string normally, but this is a much cleaner looking way of doing it in my opinion.<br /><br />The only downside I readily see is that it might not fit in look-and-feel wise with the rest of Java, but there's always ample time to get used to new things.Anonymoushttps://www.blogger.com/profile/00201436037757509576noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-51804943941829974602013-10-28T14:04:54.313+00:002013-10-28T14:04:54.313+00:00here's a use-case: Suppose you want to pass s...here's a use-case: Suppose you want to pass source code from Java to another compiler. e.g .SQL. Suppose I define a simple table:<br /><br />create table foo (<br /> bar int<br /> ,fum varchar(200)<br />);<br /><br />Now, I can paste that into some SQL IDE and run it more or less as is. If I were passing it to the DBMS via Java, I'd like to do something like this:<br /><br />String ddl = <br /> create table foo (<br /> bar int<br /> ,fum varchar(200)<br /> );<br /><br /><br />then I can pass the ddl in a call to the DBMS. This way, I have the same alternate-language source in Java and my favorite DB IDE. Same spacing and newlines. The only thing I'd want Java to do in addition is provide a way to strip the common leading spaces (four, in this case). That's not important for SQL of course, but could be in other, indentation-sensitive languages (Python, e.g.)<br /><br /><br />Gerald Britton, MCSA SQL Server, MVPhttps://www.blogger.com/profile/01158676962843150523noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-64436984069203066592013-08-23T03:18:42.073+01:002013-08-23T03:18:42.073+01:00Ridiculous that Java didn't add this in Java 7...Ridiculous that Java didn't add this in Java 7. Should definitely be in Java 8, but won't be. No wonder most people complain that Java is falling behind.Bret Ghttps://www.blogger.com/profile/06260313971702413119noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-1793707055467083932012-12-28T08:25:22.461+00:002012-12-28T08:25:22.461+00:00Why not handle a newline like any other character?...Why not handle a newline like any other character? So i would like to see:<br /><br />String a = "this is simple text<br />which continues in other line<br />and not much more to say!";<br /><br />Too simple, right?<br /><br />Now (December 2012) even a \n\ with line break is not possible.<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-75441677555626183202008-02-14T12:09:03.000+00:002008-02-14T12:09:03.000+00:00One area I'd be cautious of when sorting out a...One area I'd be cautious of when sorting out a syntax is the compilers now used within IDEs such as Eclipse.<br /><br />Now, if someone forgets to terminate a string, then the newline in the source is detected by the compiler, and allows the rest of the code to still be compiled, thus not generating a whole host of compile errors for one missing double quote.<br /><br />In this respect, I favour a syntax where the beginning of the line is marked on each line, as that would clearly allow the compiler to know that the string has not been terminated correctly.<br /><br />i.e. I much prefer:<br /><br />String a = """blah<br /> """ de<br /> """blah""";<br /><br />instead of:<br /><br />String a = """blah<br /> de<br />blah""";<br /><br />However, it would also be nice to be able to just paste text in.<br /><br />Perhaps the best approach is to just copy an existing pattern that works well... hence copying Groovy/ScalaNeale Upstonehttp://www.whirlwindmatch.comnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-59774063754369968482008-02-09T22:17:51.000+00:002008-02-09T22:17:51.000+00:00In addition :)
before-HTML-processed value and af...In addition :)<br /><br />before-HTML-processed value and after-HTML-processed value can get both into .class files.<br />String s = """<br />this is a<br />text""");<br /><br />We must provide one new function or property to access it - getHTMLText() or getHyperText(). So<br />System.out.print(s) will print " this is a text", and<br />System.out.print(s.getHtmlText()) will print the original value.Sergeynoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-48846423343543490332008-02-09T22:08:56.000+00:002008-02-09T22:08:56.000+00:00In addition:
Treated-as-HTML text between "&...In addition:<br /><br />Treated-as-HTML text between """ will not reduce program performance, because tags can be preprocessed by Java compiler and put to .class file as an ordinary Java string.<br /><br />Most of all I need multi-line strings to write two things:<br />- SQL Queries templates<br />- Usage notes for applications launched from command line<br /><br />So, SQL syntax almost doesn't intersect with HTML tags and not sensible to spaces and tabs. For the second use of HTML format is even the preffered.Sergeynoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-83992472511613858462008-02-05T10:01:50.000+00:002008-02-05T10:01:50.000+00:00I would like to see raw Strings without the need t...I would like to see raw Strings without the need to unescape chars. Something like "" or """. Very seldom I use \n, \t, … Especially in regular expressions the usage of \\ is tedious so that s.replaceAll("\\\\", "/") can become s.replaceAll(""\\"", "/") or print("Hello\"Kitty\"") is simply print(""Hello "Kitty"""). The Multiline feature isn't that important for me. FreeMarker Templates in external files with editor support works for me.Christianhttp://www.tutego.com/noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-74857023978114041212008-02-01T20:09:28.000+00:002008-02-01T20:09:28.000+00:00My preferences have long been similar to Talden...My preferences have long been similar to Talden's ideas, but more like this:<br /><br />String u =<br />"This line doesn't have trailing whitespace<br />"But this one does... "<br />"But that wasn't the end of the String.<br />"";<br /><br />Note the needed ""; at the end to terminate the String and statement.<br /><br />Further, allow backtick (`) or something for raw Strings.Tomnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-19390028110018506932008-01-31T01:28:02.000+00:002008-01-31T01:28:02.000+00:00Why? This is really, really, REALLY trivial.Why? This is really, really, REALLY trivial.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-7113273352985368082008-01-30T02:13:40.000+00:002008-01-30T02:13:40.000+00:00What about using a 'everything after here is l...What about using a 'everything after here is literal' leading character quoted layout.<br /><br />// imagine the subsequent lines are indented to match the initiating quotes<br />String s = """<br /> """ multiline comment<br /> """<br /><br />// Imagine the subsequent lines are 'two space' indented.<br />String t =<br /> """Another multiline<br /> """String literal<br /><br />The Good<br />* Inserting the leading quotes is easy with any decent editor.<br />* Supports leading whitespace as indentation AND as String content.<br />* Highly visible<br /><br />The Bad<br />* Unusually, for java, the statement terminating semi-colon isn't present nor is a String terminator.<br />* Trailing whitespace is unprotected from editor settings unaware of the importance.<br /><br />The Unclear<br />* Should the JVM use runtime platform line separators?<br />* Should trailing triple quotes be supported for those times when you need to protect trailing whitespace?<br /><br />// Trailing quotes example ('two space' indented honest).<br />String u =<br />"""This lines doesn't have trailing whitespace<br />"""But this one does... """<br />"""But that wasn't the end of the String.Taldenhttp://talden.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-48366469758339463152008-01-29T17:34:54.000+00:002008-01-29T17:34:54.000+00:00For languages like PHP and Python, multi-line Stri...For languages like PHP and Python, multi-line Strings are (or have been) benefitial, as they usually embed with HTML as interpreted languages for Web applications. Not introducing some object-access from within multi-line Strings quite narrows the advantages, and for Java, one does not really want to "print" out HTML this way.<br />I would definitely favor some String-wide escape-escape, so one could write regular expressions more readable, but I am not sure of the real use-cases for multi-line support. To only ease XML-blobbing for test cases or to create meaningful error messages does not really sound convincing to me.Stefan Schulzhttp://jroller.com/page/jaddanoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-79915477397269926582008-01-29T17:04:44.000+00:002008-01-29T17:04:44.000+00:00I don't know about multi-line Strings, but a r...I don't know about multi-line Strings, but a regular expression literal (with the /xxx/ syntax) would be much more useful IMHO! (Sorry if this was discussed here before).BoDhttp://www.JRAF.orgnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-81878127173544863382008-01-28T23:18:14.000+00:002008-01-28T23:18:14.000+00:00Thanks for all the thoughts.
My first response to...Thanks for all the thoughts.<br /><br />My first response to those that have backed away due to the multi-line issues is don't forget the benefit of the single line no-escapes format. The would make regex much easier for starters.<br /><br />The syntax <<" ">> is a valid alternative, but I would still prefer the Scala/Groovy commonality if possible.<br /><br />The code formatting whitespace problem may just be a red herring. One solution I didn't list is just ignoring it, and coding any multi-line strings from column zero.<br /><br />More significant is Daniel's point about which newline character to use - Windows or Unix. This is a nasty problem - but how do Groovy/Scala handle it?Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-76297433522022836602008-01-28T21:26:42.000+00:002008-01-28T21:26:42.000+00:00well, a second thought, automatically trimming may...well, a second thought, automatically trimming may not be a bad idea. i mean trim all the lines by default if multiline syntax is used.afsinanoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-35618582355094111632008-01-28T21:18:10.000+00:002008-01-28T21:18:10.000+00:00I like the way Python and Groovy handle this. Seem...I like the way Python and Groovy handle this. Seems we could do the same in Java. <br /><br />Why not exactly?Rick Hightowerhttp://www.jroller.com/page/RickHighnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-28371296091455979692008-01-28T20:35:58.000+00:002008-01-28T20:35:58.000+00:00python uses fixed indentations, so it leverages th...python uses fixed indentations, so it leverages that. it is not possible in java. i think that code formatting issue (preceding white spaces) will bite in all the proposals, so i say forget it, trust your IDE in java for multi-line strings.afsinanoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-50198942067925110242008-01-28T20:33:33.000+00:002008-01-28T20:33:33.000+00:00I think this would be awesome, it is such a pain t...I think this would be awesome, it is such a pain that you have to either do concatenation with + or read it from a total separate file. There doesn't need to be any special syntax. Other languages have allowed multi line strings for ages with no problem and I have always thought it a little bit silly that java wouldn't let you do it.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-45560404841939204972008-01-28T18:50:59.000+00:002008-01-28T18:50:59.000+00:00Python (which is probably where Scala/Groovy copie...Python (which is probably where Scala/Groovy copied the syntax from) handles the multi-line issue by accepting a '\newline' as a special escape that causes the newline to be ignored. e.g.:<br /><br />String foo = """\<br />Foo<br />and<br />Bar""";Philip Jenveynoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-32946744489476524592008-01-28T18:47:43.000+00:002008-01-28T18:47:43.000+00:00You guys really worry about this? Get a freaking l...You guys really worry about this? Get a freaking life.Bill Wibblenoreply@blogger.com