2. What is Kotlin DSL ?
If you’ve ever used SQL before or Groovy
with Gradle, it means that you’ve used a
DSL language before. DSL stands for
Domain Specific Language, which is a type
of programming language that cares mainly
about the readability by enforcing the use of
declarative code with minimal boilerplate
(declare to use) instead of imperative code
(explaining how to solve a problem) but as a
drawback, it can be used only inside a
specific context or domain.
3. Kotlin is already proven to be a very powerful and safe production-ready programming
language especially. As a GPL (General Purpose Language) Kotlin has some great features
that enable writing very clean code with minimal syntax, things like Lambdas with receivers
that can be used outside of method parentheses and infix function notation. These features
permit to write DSL-like code with minimal effort.
Jetbrains’ Anko library brought the power of Kotlin DSL to clean verbose Android API
especially XML layouts.
Being built on top of the core language makes Kotlin DSL considered as an Internal DSL
which means that it does not have a syntax on its own, rather being a library that offers DSL
readability by exploiting the mother GPL language features, in this case, Kotlin.
Gradle Kotlin DSL Primer
4. Promises made by Kotlin DSL :
● A statically typed and a type-safe DSL (inherited from Kotlin)
● An enhanced IDE editing experience (inherited from Kotlin)
● Interoperability with existing scripts (as a JVM language)
● Maximum readability (as any DSL)
● Consistency and superpower (by using the same language across the project — source
code and configuration 100% Kotlin)
5. Why should I even care?
Shortly, because Groovy is a dynamically typed language, and because it’s very likely that
you have come across one of these problems when dealing with build script files for Android
:
● Very poor IDE assistant when writing a Groovy DSL script
● Performance issues*
● Errors at build runtime instead of compile-time
● Painful build script debugging experience
● Refactoring is a pain in
Reference : Medium