Skip to content

Instantly share code, notes, and snippets.

@felipecsl felipecsl/unless.kt
Last active Dec 24, 2017

Embed
What would you like to do?
Kotlin unless like Ruby just for fun
inline fun <T> unless(stmt: () -> Boolean, block: () -> T, noinline elseBlock: (() -> T)? = null) =
if (!stmt.invoke()) {
block()
} else {
elseBlock?.invoke()
}
// Use like:
unless({ foo == bar }, {
doSomething()
}, {
doSomethingElse()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.