Skip to content

Instantly share code, notes, and snippets.

@occar421
Forked from orangy/event.kt
Created March 31, 2016 12:45
Show Gist options
  • Save occar421/151bdb0076f98f732ae9ed76f1fd316e to your computer and use it in GitHub Desktop.
Save occar421/151bdb0076f98f732ae9ed76f1fd316e 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)>()
operator fun plusAssign(handler: Event<T>.(T) -> Unit) { handlers.add(handler) }
operator 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