A simple project influenced by 12 days of Kotlin - Day 02 that shows
- How can you build DSL with kotlin syntax sugar
- How can you make that DSL a JSON-like.
dependencies {
implementation(kotlin("scripting-jsr223-embeddable"))
}
In short: trailing lambda, infix, unaryPlus.
All this operations just to drop brackets! But it is sufficient enough to build readable definitions.
sandwich { with type "toasted" bread = "baguette" fillings { +"cheese" +"ham" } }
/!\ Hint: if you override String.unaryPlus() inside Sandwich class, you will have access to all its members.
In short: Tried to answer a question: Can you replace JSON with DSL?
Pros: it is feasible to do following and s
would be proper Sandwich
object:
with(ScriptEngineManager().getEngineByExtension("kts")) { val s = eval( """import kon.* sandwich { with type "toasted" bread = "baguette" fillings { +"cheese" +"ham" } """) }
Cons:
- Extra line of imports (could be auto-added)
- Obviously Not secure: the
eval()
would execute anything, no checks, no restrictions - if to be used on Android, would require heavyweight compiler-as-a-library.