Created
March 24, 2020 16:12
-
-
Save vinicius73/8337c76bb7a6d7197f5c34839efbfd01 to your computer and use it in GitHub Desktop.
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 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