Top-level functions

In our previous HTML example, we began building our HTML definition by calling a function called html:

val result = html {
...
}

That html function is a top-level function that is available in the global namespace:

fun html(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}

Notice that the name of the function corresponds to the name of the type that it returns in HTML. This is a common pattern when creating a declarative DSL as it makes the function call match the type that will be returned. This makes it easy to understand what the code is doing.