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. 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.Directly from 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);yamlp performs string interpolation from YAML files:
https://bitbucket.org/djarvis/yamlp/
I reuse my library in my text editor:
https://github.com/DaveJarvis/scrivenvar/
The text editor can interpolate strings in a variety of contexts (such as Markdown, R Markdown, XML, and R XML documents):
https://www.youtube.com/watch?v=u_dFd6UhdV8
As far as I can tell, there is no performance degradation from the recursive interpolation; the editor reads, interpolates key/value pairs, and substitutes the resulting values into R Markdown documents riddled with up to a thousand simple formulas in about 600 milliseconds (on my desktop computer).
Also, the editor provides a way to maintain the interpolated variables in a tree-like view:
https://github.com/DaveJarvis/scrivenvar/blob/master/README....
The substituted values appear in the preview pane in their final form. This is all real-time and pure Java.
A. "${label1} ${label2} ${label3} .... ${label 400}"
B. "%s %s %s ... %s".formatted(label1, label2, label23, ... label400) // Ooops, typo!
Here, have another one:
I want to have a string with this content:
"Fluffy Nero Polly<random text> ... <random text> Trexxie"
Which one is easier to figure out and debug?
A. "${cat} ${dog} ${bird} <random text> ... <random text> ${dinosaur}"
B "%s %s %s <random text> ... <random text> %s".formatted(dog, giraffe, dinosaur, ..., cat).
To me, the difference shows up more with this scenario:
A: "${v1} ${v2} ${v3} ${v4}"
B1: "%s %s %s %s".formatted(v1, v2, v3)
B2: "%s %s %s %s".formatted(v1, v2, v3, v4, v5)
B3: "%s %s %s".formatted(v1, v2, v3, v4)
A does what you tell it, whether it's wrong or right. B has a chance of warning you that the argument list and the format specifier don't match. On the other hand, B gives you two things that have to be kept in sync with each other, and A can't get out of sync, since there's only one thing.
So: Less of a chance to make the error, or more of a chance to catch it. Which is better? I lean toward A, but I will admit that it seems subjective.
B only catches mistakes in the case that the number of parameters doesn't match the number of placeholders, which isn't even possible with A. If the string you built isn't correct, that's on you in either case, and should be immediately obvious by looking at the output. So in that sense, interpolation is strictly better than `formatted` at dealing with potential mistakes.
I mean it's not ideal, but sprintf and co are only intended for relatively short text (like logging), if you have any more, use a templating language instead. Plenty of those in Java as well (JSP, Velocity, etc).
[1] https://github.com/manifold-systems/manifold/tree/master/man...
A good video on this process: https://www.youtube.com/watch?v=2y5Pv4yN0b0
String interpolation would be safer, as it would make the intent clearer, and you wouldn't risk bumping in corner cases. It would be even safer if the protocol could be overrided such that you wouldn't have to use Object#toString, but for Java that's too much already.
Java has been a very conservative language. And I understand why.
But it's funny how, for many features that were added later, people were rationalizing their absence with such lines too. E.g. we don't need anonymous functions / lambdas, as anonymous classes are enough. Well, turned out that Microsoft was right all along when they released those in J#.
Also adding a template engine is overkill for doing string concatenation.
There's nothing wrong with that. If string interpolation is really useful, then Java will add it some years down the line.
It's cheap to add features but literally impossible to remove them. There's no way to undo a mistake in language design. That's an asymmetry. I'd rather err on the side of caution than kitchen-sinking it.
I like how Java binaries still work on the latest Java, however, if distribution happens via binaries, why should the language keep source compatibility anyway?
Or, you know, the latest compiler could allow you to select the source code version you want. And automated code migration tools can work too.
---
Err'ing on the side of not getting features is why Java has lost a lot of mindshare.
Java is still super popular, but that's basically in spite of the language itself, because the language is awful.
Now is the year. String interpolation is really harmless feature.
Are you saying that they haven't been around for long or that they failed? If latter, then I respectfully disagree. Any recent JS and Python code I have seen uses them extensively. They are also a joy to use, and imho improve readability immensely. Ergonomics matter. This is one aspect of Python3 I would miss the most if I had to go back to 2.
echo "$(ls)"
in my zsh, so I'm not sure what to say, to be honest.
echo "Username is ${USER}"
As I recall (and Wikipedia seems to confirm), parameter substitutions were in the original bourne shell in 1979, so... Yeah, I'm not sure what's going on there.
(Sorry, being somewhat lazy, I could pull up the spec).
> Incidental white space surrounding the content, introduced to match the indentation of Java source code, is removed.
I like how Scala does this via the indent marker and strip.
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).> 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.
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!
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.At the same time I'm slightly scared of that magic. It helps that it's well-defined in the JEP.
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
Functional style is cool and some people will go to great lengths to use it.
Like shaving a few characters of a perfectly fine piece of code by using currying and instead make one single long line of code describing it.
Can be very simple and elegant, like a better Python, on the JVM.
But also very rigorous, as a Java superset, with powerful features like mixing dynamic and static compilation in the same class.
Or even deep, with run-time metaprogramming and compile-time AST manipulation capabilities (giving tools like Gradle, Spock...).
And you have of course multiline interpolated Strings. And so much.
My absolute favorite thing is being able to do something like:
someIntegerCollection = someStringCollection*.toInteger()
I also find the use of closures for things like collection filtering to be quite nice: canadianUsers = someUserCollection.findAll { user ->
user.countryCode = "CA"
}
The equivalent in Python is a little more cumbersome and overly verbose: canadianUsers = [user for user in someUserCollection if user.countrycode = "CA"]
I'll still be writing things in Python whenever I can, but when I do write in Groovy there's a lot of nice stuff I get to have.