Kotlin is great for writing DSLs

Linas Naginionis
Jul 20, 2017 · 2 min read

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.

soundvibe

Personal blog about software development

)

Linas Naginionis

Written by

Technical Lead. Java, Kotlin, Scala, C#

soundvibe

soundvibe

Personal blog about software development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade