https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jilles Soeters

11/04/2021, 3:51 PM
Any good articles describing wrting your own DSL? I know there is a good book out there but I have too much books on my TO_READ list already
c

Casey Brooks

11/04/2021, 4:08 PM
I'm not aware of any other articles, but as a quick tip, I'd strongly encourage you to not design your library with a DSL in mind. DSLs necessarily work with mutable models which should generally be avoided, and the syntax can become pretty arcane and hard to read if you're not careful with the DSL design. Using overridden operators or infix functions might look nice if you're familiar with the domain, but it makes it difficult to understand for newcomers. Instead, first try building your API with normal immutable classes leaning on named and default parameters for easier use, and only when that becomes unwieldy should you start looking into building a dedicated DSL-style builder.
k

Kirill Grouchnikov

11/04/2021, 4:35 PM
The whole of
build.gradle.kts
is a nice custom DSL - https://docs.gradle.org/current/userguide/kotlin_dsl.html. I think the key is to keep discoverability in mind. With that, staying away from infix functions and the
+
operator would probably make your DSL more pleasant to work with
☝️ 1
2 Views