Defining a Kotlin DSL for building nested hierarchies
One of the Kotlin programming language’s strengths is the ability to easily define a language for a specific use case, or a domain-specific language (DSL). In this post, I will showcase how we can define a domain-specific language to create objects with nested hierarchies. Imagine that we have a list of authors and each author can have more than one book and each book consists of several chapters: data class Author( val id: String, val firstName: String, val lastName: String, val books: List<Book> ) data class Book( val id: String, val title: String, val chapters: List<Chapter> ) data class Chapter( val id: String, val number: Int, val title: String ) The goal of this post is to be able to easily create these objects and only provide the information that is available and have defaults for the information that is not....