<div dir="ltr"><div dir="ltr" class="gmail_attr"><div>Hello!<br></div><div><br></div><div>I think one suggestion that fixes stated problems was already proposed before and I heard no arguments against going this route.<br></div><div><br></div>On Thu, Oct 20, 2022 at 6:30 PM Jim Laskey <<a href="mailto:james.laskey@oracle.com">james.laskey@oracle.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Tagir,<br>
<br>
> On Oct 20, 2022, at 11:44 AM, Tagir Valeev <<a href="mailto:amaembo@gmail.com" target="_blank">amaembo@gmail.com</a>> wrote:<br>
> <br>
> Hello!<br>
> <br>
> As stated in JEP 430 text, the string template is a well-formed<br>
> expression of StringTemplate type. Also, toString() method of<br>
> StringTemplate prints a debug-friendly representation like<br>
> [fragments](values) which is not intended for actual use. I can<br>
> imagine that the following mistake may happen often, especially by<br>
> inexperienced developers, or developers with background in other<br>
> languages:<br>
> <br>
> int x = 10, y = 20;<br>
> System.out.println("\{x} plus \{y} equals \{x + y}");<br>
> <br>
> Output expected by programmer: 10 plus 20 equals 30<br>
> Actual output (successfully compilable): ["", " plus ", " equals ",<br>
> ""](10, 20, 30)<br>
> <br>
> As an IDE vendor, we can surely provide an inspection for some<br>
> well-known methods like PrintStream#println(Object) with a quick-fix<br>
> to add a STR. prefix. However, not everybody uses IDEs. Probably the<br>
> language itself could help here somehow? </blockquote><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> <snip><br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
</blockquote></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br><snip>   <br><br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">The problem here is we can’t have it both ways. If we treat it “like a string” in these cases then user will expect it to be a “like a string” everywhere.<br>
<br>
Another option would have been to make StringTemplate::toString generate UnsupportedOperationException, which would cause havoc for debuggers.<br>
<br>
As always, we are open to suggestions, but I think this is one of those cases where users “learn” to do the right thing.<br>
<br></blockquote><div><br></div><div>One suggestion that fixes this problem is to forbid bare template strings without policy-prefixes and instead introduce some policy that returns TemplateString itself.</div><div><br></div><div>````<br></div><div>    StringTemplate tmpl = TMPL."\{x} plus \{y} equals \{x + y}" ;</div><div>    String s1 = tmpl.apply(STR);</div><div>    String s2 = STR."\{x} plus \{y} equals \{x + y}";</div><div>    assertEquals(s1, s2);</div><div>````<br></div><div><br></div>In this world the following can be observed:<br><br>````<br>    System.out.println(STR."\{x} plus \{y} equals \{x + y}"); // Prints "10 plus 20 equals 30"<br>    System.out.println(TMPL."\{x} plus \{y} equals \{x + y}"); // Prints "StringTemplate{segments = ["", " plus ", " equals "], values = [10, 20, 30]}"</div><div class="gmail_quote">    System.out.println("\{x} plus \{y} equals \{x + y}"); // COMPILATION ERROR: error: unrecognized escape sequence '\{', policy is required for template strings</div><div class="gmail_quote">````<br><div><br></div><div>--</div><div>Victor Nazarov<br></div></div></div>