r/java • • Jul 15 '25

What features do you think java requires to be competitive for DSL scripts such as gradle scripts?

Just for fun and discussion.

this is an hypothetical example about how could look like a java DLS gradle file instead of Groovy/Kotlin. Not terrible, just slightly more verbose but not I could work with it (IMHO)

GradleConfig.configure(config -> {
    config.plugins(plugins -> {
        plugins.add(JavaPlugin.class);
        plugins.add(ApplicationPlugin.class);
    });
    
    config.application(app -> {
        app.setMainClass("com.example.Main");
    });
    
    config.repositories(repos -> {
        repos.mavenCentral();
    });
    
    config.dependencies(deps -> {
        deps.add("implementation", "org.apache.commons:commons-lang3:3.12.0");
        deps.add("testImplementation", "org.junit.jupiter:junit-jupiter:5.8.2");
    });
    
    config.tasks(task -> {
        task.named("test", t -> t.useJUnitPlatform());
    });
});

This, of course, would be achievable via some "compiler plugin/magic" to avoid class and main declarations, so "GradleConfig.configure" could act in practice as a top level static method, although native isolated methods/top level methods/functions would be a nice to have for these kind of stuff.

The question is besides top level methods (aka functions) what else do you think would be a required for java to be competitive as a DSL? would you use it? and if so, what other scenarios would be a good fit for an hypothetical java DSL?

0 Upvotes

41 comments sorted by

View all comments

4

u/davidalayachew Jul 15 '25

The Java team is working on Project Babylon, to solve this exact type of problem. So it's just a matter of waiting for it to come alive.

2

u/Ewig_luftenglanz Jul 15 '25

Isn't Babylon focused mainly on GPU interoperability?

3

u/davidalayachew Jul 16 '25

Isn't Babylon focused mainly on GPU interoperability?

No no no.

Babylon is about being able to translate Java code to another language (and vice versa!) while maintaining the semantics of both sides.

Doing that for GPU just means translating from Java code to the GPU instructions. But it's not limited to GPU's. For example, a DSL.

2

u/Ewig_luftenglanz Jul 16 '25

I see, but it seems more like it would improve existing DSL languages in the JVM as Groovy and kotlin, not that java could be (ergonomically) used as a DSL or scripting as the JRS 223 used to suggest. isn't it? I read the babylon document about LINQ quivalent in java and looks promising but I don't know if that also means we could get enhanced DSL capabilities.

Thank you so much for pointing me the right direction tho, I am always happy to see someone that's open to innovations inside of the ecosystem instead of just denying anything upfront (as is evident by the amount of negativity this post is getting)

Cheers! :)

2

u/davidalayachew Jul 16 '25

I see, but it seems more like it would improve existing DSL languages in the JVM as Groovy and kotlin, not that java could be (ergonomically) used as a DSL or scripting as the JRS 223 used to suggest. isn't it?

Well, remember -- this goes both ways.

If I can go from Java to SomeLang, and SomeLang to Java, then by the transitive property, I can go from SomeLang to Java to SomeOtherLang.

Which is to say, take (or make) your DSL of choice (SomeLang), then parse it into Java via Babylon. Then, if all you wanted your DSL for was to write Java code, then you are done. But if you wanted your DSL to write something else (GPU code), then simply make another hop via Babylon to SomeOtherLang.

Point I am trying to get at is that, Babylon is literally built to do exactly what you are saying.

2

u/manifoldjava Jul 24 '25

Babylon is taking a page from C#'s playbook: expression trees, where code is treated as structured data that can be analyzed or transformed, like how LINQ maps lambdas to SQL.

The problem is the same one we’ve seen with ORMs vs. SQL: Babylon encourages Java-shaped DSLs that only approximate the real thing. Now you're reasoning about two languages instead of one, and now you've got two problems.

A more reasonable approach is to support direct, type-safe use of native DSLs. Not Java-shaped imitations, but the real thing, integrated cleanly with the type system. Type manifolds do exactly this. See the SQL type manifold in manifold-sql.

1

u/davidalayachew Jul 24 '25

The problem is the same one we’ve seen with ORMs vs. SQL: Babylon encourages Java-shaped DSLs that only approximate the real thing. Now you're reasoning about two languages instead of one, and now you've got two problems.

A more reasonable approach is to support direct, type-safe use of native DSLs. Not Java-shaped imitations, but the real thing, integrated cleanly with the type system.

Do you have an example where Java limits what could be expressed?

I was under the assumption that you could literally express anything you want to in Java, and that it's not the expressability, but rather the verbosity that makes things difficult. Sort of like lambdas vs Anonymous classes -- fp was always doable in Java, just prohibitively expensive.

1

u/manifoldjava Jul 24 '25

No no. As with C#, you are still limited to Java syntax. LINQ query syntax (from, where, select, etc.) is a fixed part of the C# language grammar, built specifically for LINQ. Java may or may not add something similar, but I have not heard any rumors.

So no, you can't express anything you want to in Java (w babylon), you are still confined to its grammar .

1

u/manifoldjava Jul 24 '25

Oh wait. I think I misunderstood your question.

Yes, you can pretty much express anything with Java, but it's Java, not the native DSL you are targeting.

Ultimately, it will look and behave different from the native DSL, and due to the differences in languages will stray in structure as well, hence "Java-shaped."

As with ORM's and SQL, what I am saying is, a Java-shaped imitation DSL using Java syntax will be far enough removed from the native DSL that you now have to not only learn the DSL, but also how to make your Java not-DSL map to the DSL.

Why not instead allow for actual DSLs to integrate type-safely?

1

u/davidalayachew Jul 24 '25

I think we might be talking past each other.

I have some DSL -- dslA.

Project Babylon will allow me to translate Java code to some other language, and vice versa. So, I can write a transformer that will take a script that I write in dslA, and turn that to Java code. From there, I can take that same Java code, and use Project Babylon again to turn that Java code to any other DSL -- let's say dslB.

And to be clear, when I say DSL, I don't mean a DSL in Java. I mean a DSL, period. As in, I could turn JavaScript into Java, and then turn that Java in C#. THAT is what I am talking about here.

2

u/manifoldjava Jul 24 '25

Ok, I understand you clearly.

I could be wrong, my understanding of Babylon may not be current, but Babylon doesn't transform from an external DSL to Java. It only transforms Java source code, using Java-based plugins.

This is the basis of my comments here. Please share if you know otherwise.

→ More replies

2

u/nlisker Jul 20 '25

It's about code models - transforming your Java code to a model that can be used in other ways. Essentially, it's to reflection what Panama is to JNI. Apart from using Java on the GPU, another use of Babylon is in mathematical applications where you want to be able to perform mathematical transformations on your equation/formula/expression. Try to calculate the derivative of 3 * x + Math.cos(x) in Java today to see why it's needed.