Skip to content

Instantly share code, notes, and snippets.

@pksokolowski
Created May 25, 2019 17:09
Show Gist options
  • Save pksokolowski/b3e725bc1331a54a77cc13f604b8914a to your computer and use it in GitHub Desktop.
Save pksokolowski/b3e725bc1331a54a77cc13f604b8914a to your computer and use it in GitHub Desktop.
Kotlin events, with one and many listeners.
private fun raiseMoveMade() = moveMade.forEach { it.invoke() }
val moveMade = mutableListOf<() -> Unit>()
// invoking
raiseMoveMade()
// assigning a listener (in the outside world)
moveMade += { uiSounds.playMove() }
var moveMade: (() -> Unit)? = null
// invoking
moveMade?.invoke()
// assignment
moveMade = { uiSounds.playMove() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment