Skip to content

Instantly share code, notes, and snippets.

@dmitry-osin
Forked from orangy/event.kt
Created August 9, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-osin/6474f2df83f6db02ced3b9e31666c758 to your computer and use it in GitHub Desktop.
Save dmitry-osin/6474f2df83f6db02ced3b9e31666c758 to your computer and use it in GitHub Desktop.
C#-style events in Kotlin
class Event<T> {
private val handlers = arrayListOf<(Event<T>.(T) -> Unit)>()
fun plusAssign(handler: Event<T>.(T) -> Unit) { handlers.add(handler) }
fun invoke(value: T) { for (handler in handlers) handler(value) }
}
val e = Event<String>() // define event
fun main(args : Array<String>) {
e += { println(it) } // subscribe
e("sdfsdf") // invoke
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment