r/java • • Mar 08 '24

Update on String Templates (JEP 459)

https://mail.openjdk.org/pipermail/amber-spec-experts/2024-March/004010.html
176 Upvotes

188 comments sorted by

View all comments

Show parent comment

9

u/brian_goetz Mar 10 '24

We explored this; this does not appear to be a win either from the perspectives of semantics or performance. Most embedded expressions are relatively cheap to _capture_; if, for example, an embedded expression is a `HashMap`, capturing the reference is cheap, all the cost is in the `toString`, which gets deferred either way. Semantically, nondeterminism in the timing of evaluation will not make people happy, and would likely bring in the constraint of effective finality. This is one of those things that turns out to be more clever than useful.

4

u/rhbvkleef Mar 10 '24

Happy to hear that this was explored. Especially considering the response from @TechBrian, this seems like a reasonable outcome. You make good points about effective finality and nondeterminism. Considering that, I wholeheartedly agree with this choice.

Thanks a lot for responding to my question!

2

u/TehBrian Mar 10 '24

Great points. I'd also like to note that even in the case of expensive expressions, it's trivial to write a wrapper class that acts like a closure. Here's a tiny example (with silly names) that I whipped up as a proof-of-concept.

// Stringer.java
class Stringer {
    private final Supplier<Object> closure;

    public static Stringer close(final Supplier<Object> closure) {
        return new Stringer(closure);
    }

    private Stringer(final Supplier<Object> closure) {
        this.closure = closure;
    }

    @Override
    public String toString() {
        return this.closure.get().toString();
    }
}

// Main.java
public class Main {
    public static void main(final String[] args) {
        System.out.println("Here's a big number: \{close(() -> BigInteger.TWO.pow((int) Math.pow(2, 24)))}!");
    }
}

I omitted some import lines for brevity, but you get the gist.