JEP430

Remi Forax forax at univ-mlv.fr
Fri Mar 31 12:46:47 UTC 2023


> From: "Jim Laskey" <james.laskey at oracle.com>
> To: "DWR" <davidwrogers2 at gmail.com>
> Cc: "amber-dev" <amber-dev at openjdk.org>
> Sent: Friday, March 31, 2023 1:53:40 PM
> Subject: Re: JEP430

> The current design allows for you to ensure that the arguments are of a certain
> type at runtime. Compile time checks are not possible.

> import java.lang.StringTemplate.Processor;

> public class Main {
> record Complex(double r, double i) {
> public String toString() {
> return STR."[\{r} + \{i}i]";
> }
> }

> private static final Processor<String, IllegalArgumentException> DSL = st -> {
> for (Object value : st.values()) {
> if (!(value instanceof Complex)) {
> throw new IllegalArgumentException("Value is not Complex");
> }
> }

> // ...

> return StringTemplate.interpolate(st.fragments(), st.values());
> };

> public static void main(String... args) {
> Complex x = new Complex(10.0, 3.33), y = new Complex(20.0, 6.67);
> String result = DSL."\{x} + \{y}";
> System.out.println(result);
> System.out.println("Done!");
> }
> }

> ——————————————————————————

> [10.0 + 3.33i] + [20.0 + 6.67i]
> Done!

> Cheers,

> — Jim

If the code has not changed too much since the last time I checked, in term of performance, you can improve the code a bit and do the runtime check only once per callsite. 
The StringTemplate implementation class is always the same per callsite and retain the type of the captured values. 
Thus you can install an inlining cache on that class so the arguments types can be checked once per callsite. 

This is similar to the way VarH 

Rémi 

>> On Mar 27, 2023, at 9:56 PM, DWR <davidwrogers2 at gmail.com> wrote:

>> Hi, I was wondering if there is a way to constrain the type of the
>> embedded expressions? E.g. "for DSL Foo the embedded expressions
>> should all be of some type of a sealed hierarchy of Node types".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230331/e7959ba5/attachment-0001.htm>


More information about the amber-dev mailing list