October 2019
Intermediate to advanced
434 pages
11h 54m
English
Much of the power of DSLs in Kotlin comes from their type safety. When we call the html function we've been examining, we pass in a function argument with an HTML receiver. Because of Kotlin's lambda syntax, we can then move the passed function outside of the parentheses, thereby making the code easier to read and write:
val result = html { ...}
In the following snippet, we can see how the init argument is defined as a function type with a receiver of the HTML type. To indicate a function type with a receiver, we must first reference the type and then add a .(). So, in this case, HTML.() indicates a function type returning Unit with an HTML receiver:
fun html(init: HTML.() -> Unit): HTML { val html = HTML()
Read now
Unlock full access