Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Created March 24, 2020 16:12
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 vinicius73/8337c76bb7a6d7197f5c34839efbfd01 to your computer and use it in GitHub Desktop.
Save vinicius73/8337c76bb7a6d7197f5c34839efbfd01 to your computer and use it in GitHub Desktop.
package xx
class Deferrer {
private val actions = arrayListOf<() -> Unit>()
fun defer(f: () -> Unit) {
actions.add(f)
}
fun done() {
actions.reversed().forEach { action ->
action()
}
}
}
inline fun <T> withDefers(body: Deferrer.() -> T): T {
val deferrer = Deferrer()
val result = deferrer.body()
deferrer.done()
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment