Kotlin Idioms Fluent methods in Kotlin


Example

Fluent methods in Kotlin can be the same as Java:

fun doSomething() {
   someOtherAction()
   return this
}

But you can also make them more functional by creating an extension function such as:

fun <T: Any> T.fluently(func: ()->Unit): T {
    func()
    return this
}

Which then allows more obviously fluent functions:

fun doSomething() {
   return fluently { someOtherAction() }
}