Domain Specific Language in Kotlin
Introduction
Approaches to Extending Code
There may three options for extending a feature
- Change the Object Model
This has imperative code and therefore you will be forced to abstract extend, abstract extend. You be able to build quickly but it does not scale.
- External DSL (e.g. JSON)
E.g. Use json to describe the new features. You will need to extend the parser and write the code. Build will be slow but the scalability will be great
- DSL in Kotlin
Because it is just kotlin building will be quicker and it will scale hmmmmmm
Attributes of DSL Code
- Language Nature Code is meaningful and has a fluid nature
- Domain Focus DSL is focus one problem only
- Limited Expressiveness Supports only what it needs to to accomplished its task
Imperative vs Declarative
val castle = Castle()
val towerNE = Tower()
val towerSE = Tower()
val towerNW = Tower()
val towerSW = Tower()
val keep = Keep()
keep.connectTo(towerNE)
keep.connectTo(towerSE)
keep.connectTo(towerNW)
keep.connectTo(towerSW)
DSL Restricts the syntax to allow better IDE support and keep focus
castle {
keep {
to("sw")
to("nw")
to("se")
to("nw")
}
}
val castle = Castle()
val towerNE = Tower()
val towerSE = Tower()