Код IT
← Каталог

Справочник по Kotlin — DSL (Domain-Specific Languages)

Фрагмент из «Справочник по Kotlin»: DSL (Domain-Specific Languages).

Kotlin main.kt
class HTML {
    fun body(init: BODY.() -> Unit): BODY {
        val body = BODY()
        body.init()
        children.add(body)
        return body
    }
}

fun html(init: HTML.() -> Unit): HTML {
    val html = HTML()
    html.init()
    return html
}
class HTML {
    fun body(init: BODY.() -> Unit): BODY {
        val body = BODY()
        body.init()
        children.add(body)
        return body
    }
}

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