Good examples of Kotlin based DSLs

I think that kottlinx.html and tornadofx are some of the best Kotlin-based Dsls.

The one DSL I know of, which abuses the Ianguage is KotlinPoetDSL.

All those DSL’s are build on the same idea:
Create a large object which consists out of several independent objects.

TornadoFX does this by adding extension-functions to the already existing components and kotlinx.html does this by building from the ground up.
KotlinPoetDSL is build on top of KPoet. KPoet isn’t very great for building dsl’s upon, as it doesn’t treat the components as independent.

Also, the Api misuses extension-functions to make the api look like if the code was real code. You can see this when you start writing it, as you need to discover a lot of things for yourself instead of depending on the Api (for example, if you use a method, it’s just a field, which you need to invoke (this is clean in design, but not for user-friendlyness)). The api has more of this things, so writing it is definately not pretty. Reading on the other hand is…

I didn’t come across DSL’s that shouldn’t be a DSL at all, but that’s because that is probably because designing a DSL is a bit hard especially if it shouldn’t be a dsl at all.

There however are a couple of “issues” with DSL’s.

  1. reflection:

     //no Reflection
         //do something
     //with Reflection

    I think the second one is way more beatifull, but using it comes at the cost of using reflection.

  2. repeating: instead of creating an object and adding it to the parent, the paren’t should know about the child.Sometimes this can be done by implementing an interface, so you can attach the methods to that interface.
    When you can’t do that you have two options. The first is to extend the classes with a field, which they can invoke, but that has issues (topic)
    The other option is to repeat the extension-functions for every class so you repeat a lot of functions with just a diffferent receiver.

  3. slidely related, it’s possible that you have methods that you repeat in several classes, but you don’t want to expose. therefor you need an internal interface or some kind, but that’s not possible yet.

Ps. I can be this negative about the KotlinPoetDSL, because i’m the creator :wink:

1 Like