Created
October 26, 2022 14:05
-
-
Save wplong11/e327fec25fcb655e1944ac757a95153e 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
| import java.lang.Exception | |
| fun main() { | |
| runrunrun { | |
| defer { println("HI") } | |
| defer { println("HI") } | |
| defer { println("HI") } | |
| println("HELLO") | |
| println("HELLO") | |
| println("HELLO") | |
| throw Exception("HI") | |
| } | |
| } | |
| fun runrunrun(block: DeferBag.() -> Unit) { | |
| val bag = DeferBag() | |
| try { | |
| bag.block() | |
| } finally { | |
| bag.runAll() | |
| } | |
| } | |
| class DeferBag { | |
| private val list: MutableList<() -> Unit> = mutableListOf() | |
| fun defer(block: () -> Unit) { | |
| list.add(block) | |
| } | |
| fun runAll() { | |
| for (val element in list) { | |
| element() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment