<div><div dir="auto"><span style="color:rgb(49,49,49);word-spacing:1px">Hi folks,</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">Question regarding TemplateStrings that I could not seem to find the answer to in the JEP write up or the Javadoc for TemplateString: Is the expression evaluation lazy, or immediate?</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">In other words, if I do something like this:</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> List<Integer> list=new ArrayList<>();</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> TemplateString ts = "size = \{list.size()}"</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> list.add(1);</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> System.out.println(ts.apply(S</span><span style="color:rgb(49,49,49);word-spacing:1px">TR));</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">Will it print out size = 0, or size = 1?</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">My main reason for asking is that I would love for string templates to result in logging interfaces with overloaded methods that can take TemplateStrings instead of strings, and which are only evaluated if the log level is enabled. So that instead of something like this:</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> if (logger.isEnabled(Level.DEBUG)</span><span style="color:rgb(49,49,49);word-spacing:1px">) {</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> logger.log(Level.DEBUG, "Expensive value is " + lookup() );</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> }</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">we can just do:</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> log.log(Level.DEBUG, "Expensive value is \{lookup()}");</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">And be confident that the cost of invoking lookup() will only be paid if DEBUG is enabled.</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">Probably over 80% of the string building I have seen over my career has been related to building log messages. And the mistakes I have most often seen are:</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> 1. Expensive message construction not guarded by checking if logging is enabled at that level</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> 2. Expensive message construction guarded, but by checking the *wrong* logging level (e.g. check INFO, log at ERROR)</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px"> 3. Using some sort of parameterized message/string format but getting the parameters wrong (out of order, missing one, having too many, etc.)</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">If TemplateStrings evaluate their expression fragments only when processed then every one of these problems gets addressed, and logging code becomes *a lot* simpler to read, write, and code review.</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">(Random side note: I *love* the choice of \ instead of $. It took some time for me to get use to the idea, but keeping the same escape character is fantastic and I wish more languages had done that instead of using $)</span><br style="color:rgb(49,49,49);word-spacing:1px"><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">Thanks for your time,</span><br style="color:rgb(49,49,49);word-spacing:1px"><span style="color:rgb(49,49,49);word-spacing:1px">Nathan</span>
</div>
</div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div>Nathan H. Walker</div><div><a href="mailto:nathan.h.walker@gmail.com" target="_blank">nathan.h.walker@gmail.com</a></div><div>(703) 987-8937</div><div><br></div><div><br></div></div>