JEP430

Jim Laskey james.laskey at oracle.com
Fri Mar 31 11:53:40 UTC 2023


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



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/15c8ad17/attachment.htm>


More information about the amber-dev mailing list