News for hackers. Stuff that matters.
Re:JDK 15 by jen20 on Tuesday September 15, 2020 @12:08PM (#24482903)
Does 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).

Re:JDK 15 by curtis3389 on Tuesday September 15, 2020 @12:18PM (#24483036)
The common prefix is removed:

> Incidental white space surrounding the content, introduced to match the indentation of Java source code, is removed.

Re:JDK 15 by hashhar on Tuesday September 15, 2020 @12:34PM (#24483236)
I 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.

Re:JDK 15 by e12e on Tuesday September 15, 2020 @12:42PM (#24483327)
I 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

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).
Re:JDK 15 by chadrs on Tuesday September 15, 2020 @01:15PM (#24483781)
At 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/97814493...
Re:JDK 15 by electrum on Tuesday September 15, 2020 @01:25PM (#24483959)
The indentation is removed at compile time, per the JLS: 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.

Re:JDK 15 by Nullabillity on Tuesday September 15, 2020 @01:34PM (#24484086)
That's for Java, the parent is talking about Scala.
Re:JDK 15 by CydeWeys on Tuesday September 15, 2020 @03:12PM (#24485471)
It 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!

Re:JDK 15 by dfinninger on Tuesday September 15, 2020 @01:34PM (#24484095)
I 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.
Re:JDK 15 by rad_gruchalski on Tuesday September 15, 2020 @05:29PM (#24486910)
Yup, and it’s super nice for json or yaml strings.
Re:JDK 15 by electrum on Tuesday September 15, 2020 @01:18PM (#24483831)
IntelliJ 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.
Re:JDK 15 by hashhar on Wednesday September 16, 2020 @12:07PM (#24493976)
Thanks 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.

Re:JDK 15 by jen20 on Tuesday September 15, 2020 @05:19PM (#24486823)
Thanks, quote much appreciated!