<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
The current design allows for you to ensure that the arguments are of a certain type at runtime. Compile time checks are not possible.
<div><br>
</div>
<div>
<div><font face="Consolas"><span style="font-style: normal;">import java.lang.StringTemplate.Processor;</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"><br>
</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;">public class Main {</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> record Complex(double r, double i) {</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> public String toString() {</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> return STR."[\{r} + \{i}i]";</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> }</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> }</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"><br>
</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> private static final Processor<String, IllegalArgumentException> DSL = st -> {</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> for (Object value : st.values()) {</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> if (!(value instanceof Complex)) {</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> throw new IllegalArgumentException("Value is not Complex");</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> }</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> }</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"><br>
</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> // ...</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"><br>
</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> return StringTemplate.interpolate(st.fragments(), st.values());</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> };</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"><br>
</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> public static void main(String... args) {</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> Complex x = new Complex(10.0, 3.33), y = new Complex(20.0, 6.67);</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> String result = DSL."\{x} + \{y}";</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> System.out.println(result);</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> System.out.println("Done!");</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;"> }</span></font></div>
<div><font face="Consolas"><span style="font-style: normal;">}</span></font></div>
</div>
<div><br>
</div>
<div>——————————————————————————</div>
<div><br>
</div>
<div>
<div>[10.0 + 3.33i] + [20.0 + 6.67i]</div>
<div>Done!</div>
</div>
<div><br>
</div>
<div><br>
</div>
<div>Cheers,</div>
<div><br>
</div>
<div>— Jim</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
<div>
<blockquote type="cite">
<div>On Mar 27, 2023, at 9:56 PM, DWR <davidwrogers2@gmail.com> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div>Hi, I was wondering if there is a way to constrain the type of the<br>
embedded expressions? E.g. "for DSL Foo the embedded expressions<br>
should all be of some type of a sealed hierarchy of Node types".<br>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</body>
</html>