Last active
September 21, 2017 04:56
-
-
Save Atternatt/a17564e8b520e8b2a324fe927c373c3b to your computer and use it in GitHub Desktop.
Kotlin Delegation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ByDouble(incrementer: Incrementer) : Incrementer by incrementer { | |
override fun increment(number: Int): Int { | |
return super.increment(number) * 2 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ByTwo : Incrementer { | |
override val by: (Int) -> Int = { it + 2 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Incrementer { | |
val by: (Int) -> Int | |
fun increment(number: Int): Int { | |
return by(number) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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