Skip to content

Instantly share code, notes, and snippets.

@corneil
Last active August 3, 2019 21:32
Show Gist options
  • Save corneil/65ddb06d44b9f34e97d761bb344330d3 to your computer and use it in GitHub Desktop.
Save corneil/65ddb06d44b9f34e97d761bb344330d3 to your computer and use it in GitHub Desktop.
Kotlin extension functions to provide logic in association with same behaviour as apply
inline fun <T> T.ifApply(expression: Boolean, block: T.() -> Unit, otherwise: T.() -> Unit) : T {
return if(expression) {
this.apply(block)
} else {
this.apply(otherwise)
}
}
inline fun <T> T.ifApply(expression: Boolean, block: T.() -> Unit) : T {
if(expression) {
this.apply(block)
}
return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment