DEV Community

Discussion on: What are your favorite programming language syntax features?

Collapse
 
defman profile image
Sergey Kislyakov

Kotlin's function literals with receiver and infix functions are pretty cool.

Function literals with receivers:

class HTML {
    fun body() { ... }
}

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

html {
    body()
}

Infix:

infix fun Int.shl(x: Int): Int { ... }

// calling the function using the infix notation
1 shl 2

// is the same as
1.shl(2)