<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <br>
    <br>
    <div class="moz-cite-prefix">On 6/15/2023 6:01 PM, Anatoly
      Kupriyanov wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAGwStpBmGnTviAVq3J5hCzEstjVDsMpDuudNbKtSaJ4TD=FF2A@mail.gmail.com">
      
      <div dir="ltr">
        <div>I understand why it works like that but it still causes
          some confusion to me, at least it should be somehow
          documented:</div>
        <div><br>
        </div>
        <div>
          <div style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre"><span style="color:rgb(0,51,179)">var </span><span style="color:rgb(0,0,0)">val </span>= <span style="color:rgb(23,80,235)">1</span>;
<span style="color:rgb(0,51,179)">var </span><span style="color:rgb(0,0,0)">template </span>= <span style="color:rgb(135,16,148);font-style:italic">RAW</span>.<span style="color:rgb(6,125,23)">"val = </span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">\{</span><span style="color:rgb(6,125,23)">val</span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">}</span><span style="color:rgb(6,125,23)">"</span>;
<span style="color:rgb(0,51,179)">assert </span><span style="color:rgb(0,0,0)">template</span>.interpolate().equals(<span style="color:rgb(6,125,23)">"val = 1"</span>);
<span style="color:rgb(0,0,0)"></span></div>
          <div style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre"><span style="color:rgb(0,0,0)">val </span>= <span style="color:rgb(23,80,235)">2</span>;
<span style="color:rgb(0,51,179)">assert </span><span style="color:rgb(0,0,0)">template</span>.interpolate().equals(<span style="color:rgb(6,125,23)">"val = 1"</span>);<span style="color:rgb(140,140,140);font-style:italic">//weird
</span></div>
        </div>
      </div>
    </blockquote>
    <br>
    Yes, a template closes over values, not variables.  We could adopt
    the same constraint as we do with lambdas / inner classes -- require
    that any referenced variables be effectively final -- but this would
    likely be quite restrictive given the typical use cases for string
    templates.  <br>
    <br>
    <blockquote type="cite" cite="mid:CAGwStpBmGnTviAVq3J5hCzEstjVDsMpDuudNbKtSaJ4TD=FF2A@mail.gmail.com">
      <div dir="ltr">
        <div><br>
          <div>Another thing, I could not figure out a reasonable
            conditional template syntax and nesting. This looks kind of
            ugly and hard to read<br>
          </div>
          <div><br>
          </div>
          <div style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre"><span style="color:rgb(0,0,0)">String </span><span style="color:rgb(0,98,122)">msg</span>(<span style="color:rgb(0,0,0)">String </span>name, <span style="color:rgb(0,0,0)">String </span>val)</div>
          <div style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre">{
    <span style="color:rgb(0,51,179)">return </span><span style="color:rgb(135,16,148);font-style:italic">STR</span>.<span style="color:rgb(6,125,23)">"Today is a nice day. </span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">\{</span>name == <span style="color:rgb(0,51,179)">null </span>? <span style="color:rgb(6,125,23)">"" </span>: <span style="color:rgb(135,16,148);font-style:italic">STR</span>.<span style="color:rgb(6,125,23)">"Hello </span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">\{</span><span style="color:rgb(6,125,23)">name</span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">}</span><span style="color:rgb(6,125,23)">! "</span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">}</span><span style="color:rgb(6,125,23)">You have </span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">\{</span><span style="color:rgb(6,125,23)">val</span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">}</span><span style="color:rgb(6,125,23)"> thing."</span>;</div>
          <div style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre">}</div>
          <div style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre">
</div>
          <div>
            <div style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre"><span style="color:rgb(0,51,179)">assert </span>msg(<span style="color:rgb(0,51,179)">null</span>, <span style="color:rgb(6,125,23)">"this"</span>).equals(<span style="color:rgb(6,125,23)">"Today is a nice day. You have this thing."</span>);
<span style="color:rgb(0,51,179)">assert </span>msg(<span style="color:rgb(6,125,23)">"World"</span>, <span style="color:rgb(6,125,23)">"that"</span>).equals(<span style="color:rgb(6,125,23)">"Today is a nice day. Hello World! You have that thing."</span>);
</div>
          </div>
          <div><br>
          </div>
          <div>This thing would be especially important e.g. for a JSON
            tree templating.<br>
          </div>
          <br>
        </div>
      </div>
    </blockquote>
    <br>
    Whenever you have an expression that is so complicated or deeply
    nested that it is hard to read, the standard trick is to break it
    up.  This is not unique to string templates.  <br>
    <br>
        var n = (<span style="color:rgb(6,125,23);background-color:rgb(255,204,204)"></span>name
    == <span style="color:rgb(0,51,179)">null) </span>? <span style="color:rgb(6,125,23)">"" </span>: <span style="color:rgb(135,16,148);font-style:italic">STR</span>.<span style="color:rgb(6,125,23)">"Hello </span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">\{</span><span style="color:rgb(6,125,23)">name</span><span style="color:rgb(6,125,23);background-color:rgb(255,204,204)">}</span><span style="color:rgb(6,125,23)">! ";<br>
          return STR."Today is a nice day. \{n}  You have \{val}
      thing.";<br>
      <br>
      <br>
    </span>
  </body>
</html>