3 ms·
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/
by e12e 6y ago
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 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!