Skip to content

Instantly share code, notes, and snippets.

@Atternatt
Last active September 21, 2017 04:56
Show Gist options
  • Save Atternatt/a17564e8b520e8b2a324fe927c373c3b to your computer and use it in GitHub Desktop.
Save Atternatt/a17564e8b520e8b2a324fe927c373c3b to your computer and use it in GitHub Desktop.
Kotlin Delegation Example
class ByDouble(incrementer: Incrementer) : Incrementer by incrementer {
override fun increment(number: Int): Int {
return super.increment(number) * 2
}
}
class ByTwo : Incrementer {
override val by: (Int) -> Int = { it + 2 }
}
interface Incrementer {
val by: (Int) -> Int
fun increment(number: Int): Int {
return by(number)
}
}
ByDouble(ByTwo()).increment(10)
//devuelve 24 -> (10+2) * 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment