Kotlin is great for writing DSLs
Kotlin is the rising star of the new programming languages. After Google has announced that it is officially supporting Kotlin as a first-class language in Android development, it’s interest has grown drastically.
One of the most useful things which I find in Kotlin is how fun and easy you can write your own DSLs.
Just recently I tried to write a simple, type-safe Json builder using Kotlin. My goal was to make it look as close to real Json as possible.
Eventually my efforts converged into new small library, which I named jkob (I know I’m bad at naming things…)
So how you should build json document using jkob?
It looks almost like real json and it’s 100% compiled, type-safe Kotlin code.
If only Kotlin supported collection literals, this code would be even simpler. There a many discussions in Kotlin community whether collection literals should be added to the language. Personally, I wouldn’t mind to have them if somehow you could control which implementation should be used when initializing collections. I don’t think that forcing everyone to use internal ArrayList or HashSet implementation is the way to go.
So, in conclusion, I was having real fun while I was writing this small library and I am keen to write more Kotlin code in future.