Created
October 2, 2019 12:42
-
-
Save bearoffwork/a3fa67e5771441f4942840884b802cca to your computer and use it in GitHub Desktop.
Generic extension function in Kotlin
This file contains hidden or 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
package io.rra3b.kotlin | |
inline fun <T, reified ProcessorOfT : AbstractDslStyleProcessor<T>> ProcessorOfT.process( | |
t: T, | |
processorOptions: ProcessorOfT.() -> Unit | |
) { | |
this.apply(processorOptions).process(t) | |
} | |
abstract class AbstractDslStyleProcessor<T> { | |
//region callbacks | |
var onSuccess: (() -> Unit)? = null | |
fun onSuccess(onSuccess: (() -> Unit)) { | |
this.onSuccess = onSuccess | |
} | |
var onComplete: (() -> Unit)? = null | |
fun onComplete(onComplete: (() -> Unit)) { | |
this.onComplete = onComplete | |
} | |
//endregion | |
abstract fun process(t: T) | |
} | |
class AProcessor : AbstractDslStyleProcessor<A>() { | |
override fun process(t: A) { | |
TODO("not implemented") | |
} | |
} | |
class BProcessor : AbstractDslStyleProcessor<B>() { | |
override fun process(t: B) { | |
TODO("not implemented") | |
} | |
} | |
data class A(val dummy: Unit) | |
data class B(val dummy: Unit) | |
This file contains hidden or 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
package io.rra3b.kotlin | |
fun main() { | |
val a = A(Unit) | |
val b = B(Unit) | |
val aProcessor = AProcessor() | |
val bProcessor = BProcessor() | |
aProcessor.process(a) { | |
onSuccess { | |
// do something | |
} | |
onComplete { | |
// do something | |
} | |
} | |
bProcessor.process(b) { | |
onSuccess { | |
// do something | |
} | |
onComplete { | |
// do something | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment