Model Train-related Notes Blog -- these are personal notes and musings on the subject of model train control, automation, electronics, or whatever I find interesting. I also have more posts in a blog dedicated to the maintenance of the Randall Museum Model Railroad.

2022-04-25 - Conductor 2: Kotlin DSL vs Groovy DSL

Category Rtac

Over the last few months, I re-implemented my Conductor 2 prototype entirely. The initial Groovy DSL project lacked a clear structure, so I scratched it. And since I was rewriting it from scratch, I explored using a Kotlin DSL instead.

The verdict is to go with the Kotlin DSL approach.

Highlighting the pros and cons:

  • Pro for Groovy: I much prefer the parenthese-free syntax available in a Groovy DSL.
    • Groovy makes for a really elegant DSL.
  • Cons for Kotlin: The DSL syntax is more clumsy, requiring parentheses for function calls, etc.
    • Generally speaking the Kotlin syntax is a lot more arcane (which is probably why a lot of people think it’s “cool”... it’s not).
  • Cons for Groovy: I cannot find a way to avoid leaking implementation classes into the DSL.
    • I want to expose only pure interfaces, yet somehow I can always manage to trivially work around an interface and find its underlying “private” method or properties in a script. The Groovy runtime happily gives me access to everything. It’s ironically too flexible.
    • In Kotlin, I can use “internal” classes to hide them effectively and the compiler prevents me from using them in the script.
  • Pros for Kotlin: I get a lot more compile-time errors and warnings, and the IJ integration with KTS is supreme.
  • Cons for Kotlin: The entire KTS feature is marked experimental and even Kotlin advocates/fanboys online admit that it’s not well maintained/supported. That’s a risk to take.

So far the cons is a bit more clumsy syntax with more parentheses, yet that is consistent.

There’s a noticeable loading time when loading the script -- likely a compilation phase -- however that will only be done once at startup so it can be acceptable. I need the full-size script to understand if that time is linear or becomes unreasonable.

One of the reasons for choosing Kotlin is that so far the DSL interface is more easily sanitized. With Groovy, too much of the implementation leaks into the DSL with no obvious way to prevent access. With Kotlin only whatever is exposed in an interface is accessible (although of course reflection could be used, but we are not to prevent that).

Loading time:

  • Groovy DSL: 2.9s
  • Kotlin DSL: 12s

 Generated on 2022-10-03 by Rig4j 0.1-Exp-666f4a7