Enhancing the DSL (optional features)
Our current DSL is quite functional and readable. However, Kotlin offers more features that could be used to refine or extend DSLs.
Infix functions for property assignment (alternative style)
For some DSLs, using infix functions for assignments can make the syntax feel even more like a natural language. For example, instead of name = "Alex Koder", you might want name to "Alex Koder".
To achieve this, you could define an infix extension function:
// A generic infix function for assignment-like operations in builders
infix fun <T> Property<T>.to(value: T) {
this.set(value)
}
// Helper class to wrap properties, needed for the 'to' infix function
class Property<T>(private val setter: (T) -> Unit) {
fun set(value: T) = setter(value)
}
// Modify AuthorBuilder (and others)
class AuthorBuilder {
private var _name: String = ""
private var _country: String = "...