10 ms·
I'm so happy that we finally have multiline text blocks in Java: String query = """ SELECT "EMP_ID", "LAST_NAME" FROM "EMPLOYEE_TB"
by kentosi 6y ago
I'm so happy that we finally have multiline text blocks in Java:
String query = """
SELECT "EMP_ID", "LAST_NAME" FROM "EMPLOYEE_TB"
WHERE "CITY" = 'INDIANAPOLIS'
ORDER BY "EMP_ID", "LAST_NAME";
""";
It's one more step closer to scala/kotlin/etc.
- jen20 6y agoDoes this result in a string with a hanging indent, or is the common prefix removed? (Sorry, being somewhat lazy, I could pull up the spec).
- curtis3389 6y agoThe common prefix is removed: > Incidental white space surrounding the content, introduced to match the indentation of Java source code, is removed.
- hashhar 6y agoI hate such kind of magic. Very convenient at first but can cause countless hours spent on debugging in some cases. I like how Scala does this via the indent marker and strip.
- e12e 6y agoI was going to answer gp "probably not" - but I would've been wrong. It looks to me like Java now has some of the most sophisticated in-line multi-line strings/here-documents that I'm aware of in any language. More details in: https://openjdk.java.net/jeps/378 https://openjdk.java.net/jeps/378 I'm not entirely sure "most sophisticated" is entirely a good thing - but the jep looks pretty thorough at least. Ed: how does Scala do it? Java uses the indents for the closing triple-quote to determine stripping/indent + cutting trailing spaces. Ie, if my reading is correct: a = """ OK. """ Is "OK.\n" (even if there are any spaces after the dot).
- chadrs 6y agoAt the string literal level doesn't do anything, but the standard library adds an extension method to string called `stripMargin`. This has some nice examples https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch01s03.html https://www.oreilly.com/library/view/scala-cookbook/97814493...
- electrum 6y agoThe indentation is removed at compile time, per the JLS: https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10.6 https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.htm... > Incidental white space is removed, as if by execution of String.stripIndent on the characters resulting from step 1. The new stripIndent() method is provided as a convenience to developers.
- Nullabillity 6y agoThat's for Java, the parent is talking about Scala.
- dfinninger 6y agoI had to use this recently for an online course. It was super nice: def pushConstant(num: Int): String = s""" |@$num |D=A |$accessTopOfStack |M=D |$incrementStackPointer |""".stripMargin accessTopOfStack and incrementStackPointer are other functions that return text. It made composing Assembly a breeze.
- rad_gruchalski 6y agoYup, and it’s super nice for json or yaml strings.
- CydeWeys 6y agoIt helps that Java is exclusively a compiled programming language, so these are just syntactic sugar concerns for the parser/lexer and the compiled bytecode ends up looking identical to having just used one long string with inline \n. So given that, I don't think it really ends up being that sophisticated. I think this could be a decent easy programming question for a technical interview though!
- electrum 6y agoIntelliJ shows you exactly where the block starts, so if you have a line with less indentation (causing the entire block to start at that column), it is easy to spot.
- hashhar 6y agoThanks for sharing that. I can definitely see myself using the multi-line blocks since they are much better than the kind of things we need to do today. At the same time I'm slightly scared of that magic. It helps that it's well-defined in the JEP.
- jen20 6y agoThanks, quote much appreciated!
- 60secz 6y agoShame they don't actually include templating inside of the string literal. Still have to append with a lot of tokenized replaces. Since they've committed to no formatting in the literal section, will be interesting to see if they eventually support a syntax like python's f-strings: f"""${foo}"""
- johnnycerberus 6y agohttp://manifold.systems/ http://manifold.systems/ has a Java template engine that can be helpful
- 60secz 6y agoWill definitely check it out!
- carimura 6y agoMaybe you're looking for String::formatted? Directly from http://openjdk.java.net/jeps/378 http://openjdk.java.net/jeps/378: Another alternative involves the introduction of a new instance method, String::formatted, which could be used as follows: String source = """ public void print(%s object) { System.out.println(Objects.toString(object)); } """.formatted(type);
- dtech 6y agoThis was already there, and is much less user-friendly than string interpolation, because it's easy to mess up order
- mumblemumble 6y agoI'd still prefer honest-to-goodness string interpolation. Formatting functions with positional placeholders need a linter to be kept (vaguely) maintainable, and I suspect that formatting functions with named placeholders would be difficult to make acceptably ergonomic or performant in Java.
- johnnycerberus 6y agoI think we can do better than Java, Scala and Kotlin. All of these ended up to be piles of abstractions and complexity. Scala went too hardcore for somebody that just wants to deliver. Kotlin gives you anxiety everytime they push for supporting a new platform. Java, though it is still nice, has accumulated too much cruft during the years. I wish for a Go-like language on the JVM. A modern language that offers you a minimum number of features from which you can build upon. Moreover, lower, well-thought out abstractions would give a boost in perfomance with the new garbage collectors that have been added in the JVM. I can imagine Clojure is something that I would like to try again sometimes, but the fact that it is a LISP and lacks even simple types make it less attractive to the majority of devs. Also, Clojure wouldn't be my choice for high-perfomance if I want to do mostly imperative stuff.
- weego 6y agoScala is perfect for just delivering, myself and my business partner have been using it to rapidly prototype and build for a decade now. You just avoid all the stuff that tries to make scala into haskell and use it as the hybrid OO/functional language is was designed as.
- johnnycerberus 6y agoYou're lucky that you can do that kind of work. I've been working for a state company in Bulgaria and we had to maintain some Scala pipelines built with Apache Spark some years ago. It wasn't a pleasant experience, just a sea of vars/vals and cases of reinventing the wheel only to use some functional constructs when it could be simply done in Java by just adding a dependency that has already solved that problem like 10 years ago. It wasn't even some complicated logic in that, just calling different APIs and playing with dataframes, but the fact that they hid from me so much behavior only to reduce verbosity gave me some stressful days.
- non-entity 6y agoAny partitular reason scala was chosen? Spark has Python and Java API's as well.
- agumonkey 6y agoand closer to humans
- dxxvi 6y agoI'm waiting for Try and Either in Java. Multiline text blocks are good but not enough.
- kelnos 6y agoIf you want those, you can use Vavr[0]. Ultimately the issue isn't the presence or absence of those classes (because they aren't hard to write minimal versions of as a one-off if you need them and can't allow the dependency), it's the pervasiveness of use (or lack thereof) by libraries and especially the standard library. If Try and Either were added to the Java stdlib tomorrow, the rest of the stdlib would still be stuck throwing exceptions for error propagation, because they can't change the return types of existing methods. The only benefit would be that third-party libraries could feel comfortable enough to use them in their type signatures. But, in practice, most libraries still target Java 8, so I wouldn't hold my breath for that, either. [0] https://vavr.io https://vavr.io
- jsumrall 6y agoIf you’re writing code like this I can highly recommend http://www.jooq.org/ http://www.jooq.org/