Ivan Ponomarev
![]() | Ivan Ponomarev
|
Code can be written by a subject-matter expert, not a programmer
Sometimes code can be written by a subject-matter expert, not a programmer
Often code can be read by a subject-matter expert
Sometimes code can be written by a subject-matter expert, not a programmer
Often code can be read by a subject-matter expert
DSL code is the single source of truth for many tasks/aspects of the project
![]() | Martin Fowler, Rebecca Parsons. Domain-Specific Languages ![]() |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
* Does not mean that you are safe, e.g. google for "Billion_laughs_attack"
Tool | DSL syntax | General syntax |
Extension functions |
|
|
Infix functions |
|
|
Operators overloading |
|
|
Type aliases |
| Creating empty inheritors classes and other duct tapes |
Tool | DSL syntax | General syntax |
get/set methods convention |
|
|
Destructuring declaration |
|
|
Lambda out of parentheses |
|
|
Lambda with receiver |
| N/A |
Context control |
| N/A |
Execution of rules
Documentation
Visualization
Validation
Serialization ("free" JSON/YAML based DSL version for our Kotlin DSL)
We can leave extension points in our builder:
//In our example
customCondition { Random.nextDouble() < .88} invokes TransformationC
//Or in some other DSL
customBusinessRule { checkSmthProgramatically() }
Great for describing business rules (state-transition model, for example), especially if DSL defines only part of rules, which is usually the case.
this
brightonKotlin {
//Cannot get rid of paretheses (unlike Groovy)
talk ("Talk 1") deliveredBy {
speaker ("Speaker 1")
speaker ("Speaker 2")
}
talk ("Talk 2") deliveredBy {
speaker("ssd")
speaker ("Speaker 3")
}
}
this
: a workaround brightonKotlin {
//Cannot get rid of paretheses
+ "Talk 1" deliveredBy {
+ "Speaker 1"
+ "Speaker 2"
}
+ "Talk 2" deliveredBy {
+ "ssd"
+ "Speaker 3"
}
}
talk ("Talk 1") deliveredBy {
talk (...) // ???
}
@DslMarker
annotation class MeetupDsl
@MeetupDsl
class MeetupBuilder { ... }
@MeetupDsl
class SpeakersBuilder { ... }
![]() |
|
Gradle Kotlin DSL
Ktor Framework: https://ktor.io/
Exposed (an ORM for Kotlin): https://github.com/JetBrains/Exposed?tab=readme-ov-file#examples
DSL combined with designed patterns is a powerful tool for solving multiple tasks
Creating DSLs in Kotlin is not scary. You can improve parts of your existing internal APIs today making them "DSL-like"
Internal Kotlin DSLs are not the only way to implement DSLs, but definitely not the worst one in many scenarious.
Code and slides are available on GitHub https://github.com/inponomarev/dsl-talk | @inponomarev |