Advanced techniques for building Kotlin DSL(s)
summary
Shows how to implement a Kotlin DSL that generates Starlark (Bazel) code by building an AST of typed nodes and emitting formatted files, used in Project Iron for Gradle→Bazel migration. Uses lambda with receiver entry points, extension and backticked operator functions to mimic index/assignment/plus/slice syntax, DSL markers and per-feature interfaces to avoid context leaks, and KSP to auto-generate boilerplate for functions with ~10–15 arguments. Produces real Starlark output and is applied successfully in the migration project.
AI-written summary · about
chapters (13)
- 0:00Introduction to Kotlin DSLs
- 6:31Entering DSL context in Kotlin
- 9:41Implementing indexed access operators
- 11:05Using property delegation for assignments
- 14:33Dynamic function arguments
- 17:01Custom function calls with lambdas
- 18:27Handling concatenation operations
- 20:52Preventing context conflicts with DSL markers
- 21:58Structuring DSL with feature interfaces
- 24:07Additional DSL features like list comprehensions
- 25:54Automating DSL generation with KSP
- 27:40Real-world DSL example in Android projects
- 30:10Key takeaways and experimentation
related talks
30mKotlin DSL for Android Projects
53mDeveloping DSLs in a pragmatic way
33mGradle with Kotlin
44mBuilding Languages Using Kotlin
42mWhen your DSL Needs to Support User-Defined Functions
44mFurther with Kotlin!
39mDiving into advanced Kotlin features
39mAutomated migration of Android apps to Bazel build system
