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).
Directly from http://openjdk.java.net/jeps/378: